Ubuntu 16.04 + Nginx + PHP7.2 高性能服务器快速配置
- 添加PHP7.2的源
sudo apt-get install -y python-software-properties software-properties-common
sudo add-apt-repository -y ppa:ondrej/php && sudo apt-get update
leo@ulinux:~$ sudo apt-get install -y python-software-properties software-properties-common
leo@ulinux:~$ sudo add-apt-repository -y ppa:ondrej/php && sudo apt-get update
- 检查一下添加的是否正确
apt-cache pkgnames | grep php7.2
leo@ulinux:~$ apt-cache pkgnames | grep php7.2
- 安装 nginx + php7.2
sudo apt-get install -y nginx php7.2 php7.2-bz2 php7.2-common php7.2-cli php7.2-dba php7.2-dev php7.2-bcmath php7.2-fpm php7.2-gmp php7.2-mysql php7.2-tidy php7.2-sqlite3 php7.2-json php7.2-opcache php7.2-sybase php7.2-curl php7.2-ldap php7.2-phpdbg php7.2-imap php7.2-xml php7.2-xsl php7.2-intl php7.2-zip php7.2-odbc php7.2-mbstring php7.2-readline php7.2-gd php7.2-interbase php7.2-snmp php7.2-xmlrpc php7.2-soap php7.2-pspell php7.2-pgsql php7.2-enchant php7.2-recode
leo@ulinux:~$ sudo apt-get install -y nginx php7.2 php7.2-bz2 php7.2-common php7.2-cli php7.2-dba php7.2-dev php7.2-bcmath php7.2-fpm php7.2-gmp php7.2-mysql php7.2-tidy php7.2-sqlite3 php7.2-json php7.2-opcache php7.2-sybase php7.2-curl php7.2-ldap php7.2-phpdbg php7.2-imap php7.2-xml php7.2-xsl php7.2-intl php7.2-zip php7.2-odbc php7.2-mbstring php7.2-readline php7.2-gd php7.2-interbase php7.2-snmp php7.2-xmlrpc php7.2-soap php7.2-pspell php7.2-pgsql php7.2-enchant php7.2-recode
- 安装 Mcrypt 扩展
sudo apt-get install libmcrypt-dev
sudo pecl install mcrypt-1.0.1
sudo su
echo "extension=mcrypt.so" > /etc/php/7.2/mods-available/mcrypt.ini
phpenmod -v 7.2 mcrypt
leo@ulinux:~$ sudo apt-get install libmcrypt-dev
leo@ulinux:~$ sudo pecl install mcrypt-1.0.1
leo@ulinux:~$ sudo su
root@ulinux:/home/leo# echo "extension=mcrypt.so" > /etc/php/7.2/mods-available/mcrypt.ini
root@ulinux:/home/leo# phpenmod -v 7.2 mcrypt
- 安装 Redis 扩展
sudo apt-get install php-igbinary
sudo pecl install redis
sudo su
echo "extension=redis.so" > /etc/php/7.2/mods-available/redis.ini
phpenmod -v 7.2 redis
leo@ulinux:~$ sudo apt-get install php-igbinary
leo@ulinux:~$ sudo pecl install redis
leo@ulinux:~$ sudo su
root@ulinux:/home/leo# echo "extension=redis.so" > /etc/php/7.2/mods-available/redis.ini
root@ulinux:/home/leo# phpenmod -v 7.2 redis
enable igbinary serializer support? [no] : y
enable lzf compression support? [no] : y
- 安装 Memcached 扩展
sudo apt-get install zlib1g-dev libmemcached-dev
sudo pecl install memcached
sudo su
echo "extension=memcached.so" > /etc/php/7.2/mods-available/memcached.ini
phpenmod -v 7.2 memcached
leo@ulinux:~$ sudo apt-get install -y zlib1g-dev libmemcached-dev
- 安装 Swoole 扩展
sudo pecl install swoole
sudo su
echo "extension=swoole.so" > /etc/php/7.2/mods-available/swoole.ini
phpenmod -v 7.2 swoole
leo@ulinux:~$ sudo pecl install swoole
leo@ulinux:~$ sudo su
root@ulinux:/home/leo# echo "extension=swoole.so" > /etc/php/7.2/mods-available/swoole.ini
root@ulinux:/home/leo# phpenmod -v 7.2 swoole
- 安装 Imgemagick 扩展
sudo apt-get install imagemagick php-imagick
leo@ulinux:~$ sudo apt-get install imagemagick php-imagick
- 优化配置 Nginx
/etc/nginx/nginx.conf
worker_processes 2;
worker_cpu_affinity 01 10;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
types_hash_max_size 2048;
server_tokens off;
server_names_hash_bucket_size 64;
server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 4k;
gzip_http_version 1.1;
gzip_types text/html text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-available
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
- Linux 内核优化
追加以下配置到系统的 /etc/sysctl.conf
文件中,然后使用如下命令使修改生效:/sbin/sysctl -p
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
- PHP-FPM 优化
修改 /etc/php/7.2/fpm/pool.d/www.conf
修改 backlog = 8192
listen.backlog = 8192
- 虚拟主机优化
修改 Nginx 虚拟主机配置, 追加 backlog 参数
...
listen 80 default_server backlog=8192;
listen [::]:80 default_server backlog=8192;
...