NGINX在性能,潜在的缓解攻击和资源使用方面优于大多数其他Web服务器。 但是很多人很难让一切正常运转,并且有充分的理由。 许多为您或其他指南管理Nginx的安装脚本可能会为您提供有关如何安装过时版本的步骤,这些版本可能缺少重要的安全更新或性能更改,或者是在浏览器中呈现服务器错误的PHP配置。 我们测试了我们的并确保它在这种环境中工作。

操作系统: CentOS 7 (7.5) x86_64

要安装的软件堆栈:
- NGINX 主线 (最新 - CHANGELOG)
- PHP 7.3, PHP-FPM (via Remi repository)
- LetsEncrpt SSL

在本指南中,我们新安装的CentOS 7.5 VPS之一正在使用SELinux禁用。


安装 PHP 7.3 from Remi 存储库



• 安装 yum-utils 为了使用 yum-config-manager 工具
yum install -y yum-utils

• 安装 Epel 和 Remi repositories
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm

• 启用 Remi repository
yum-config-manager --enable remi-php73

注意: 你可以改变 "php73" 至 "php72" (或其他版本) 如果此处的版本不支持您需要的模块,请在本指南中使用不同的PHP版本。
 
• 运行更新然后安装PHP包
yum update -y
yum install -y php73 php73-php-fpm

• 我们必须编辑 php.ini 配置这个PHP安装 (用户任何文本编辑器,我们使用nano)
nano /etc/opt/remi/php73/php.ini
更换:
;cgi.fix_pathinfo=1
同:
cgi.fix_pathinfo=0
删除分号, 将1更改为0

• 现在我们需要编辑配置 PHP-FPM
nano /etc/opt/remi/php73/php-fpm.d/www.conf
更换:
listen = 127.0.0.1:9000
:
listen = /var/run/php73-fpm/php73-fpm.sock

• 现在编辑相同的文件,在配置中较低,您需要更改以下内容
nano /etc/opt/remi/php73/php-fpm.d/www.conf
更换:
;listen.owner = nobody
;listen.group = nobody
:
listen.owner = nginx
listen.group = nginx
(删除分号并将'nobody'更改为'nginx')您需要更改以下内容

• 现在再次编辑同一文件,在配置顶部附近,您需要更改以下内容
nano /etc/opt/remi/php73/php-fpm.d/www.conf
更换:
user = apache
group = apache
:
user = nginx
group = nginx

• 现在创建套接字文件的目录
mkdir /var/run/php73-fpm

• 更改会话目录的文件权限,以便PHP会话正常工作
chown -R nginx:nginx /var/opt/remi/php73/lib/php/session

• 安装NGINX之后,我们可以启动PHP-FPM,如果我们现在尝试它会发出错误,因为尚未创建“nginx”系统用户
systemctl restart php73-php-fpm
systemctl enable php73-php-fpm
注意:在启动PHP-FPM之前,您需要先安装Nginx。


从Nginx存储库安装Nginx主线



• 首先,我们必须添加NGINX PGP密钥以验证完整性并确认软件包的来源
wget http://nginx.org/keys/nginx_signing.key
rpm --import nginx_signing.key && rm -rf nginx_signing.key

• 现在添加NGINX存储库,创建一个新文件
nano /etc/yum.repos.d/nginx.repo
添加以下内容并保存:
[nginx]
name=nginx
baseurl=http://nginx.org/packages/mainline/centos/7/x86_64/
gpgcheck=1
enabled=1

• 现在安装NGINX主线
yum update -y
yum install -y nginx

• 启动NGINX并在启动时启用它
systemctl start nginx
systemctl enable nginx

• 检查NGINX版本和状态以确认
systemctl status nginx && nginx -v

• 现在,我们必须为您的域创建新的虚拟主机配置。 请务必使用您自己的域替换“example.com”的所有实例。
nano /etc/nginx/conf.d/example.com.conf
粘贴以下内容:
server {
  listen 80;
  server_name www.example.com example.com;
  root /usr/share/nginx/example.com;
  index index.php index.html index.htm;
  
  location / {
    try_files $uri $uri/ /index.php$query_string =404;
  }
  
  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.htm {
    root /usr/share/nginx/example.com;
  }
  
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php73-fpm/php73-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

• 现在重启NGINX和PHP-FPM
systemctl restart nginx
systemctl restart php73-php-fpm


• 现在我们将为虚拟主机创建一个web目录,并创建一个索引文件和一个php infofile来检查php处理。
mkdir /usr/share/nginx/example.com
echo "test index" >> /usr/share/nginx/example.com/index.html
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/example.com/info.php

现在,在您的网络浏览器中访问您的域名,并确保一切正常。 您可以转到您的域/info.php来检查PHP模块和参数

• 要在将来将NGINX更新为新的主线版本,只需运行以下命令即可
yum update nginx -y
systemctl restart nginx

• 如果您使用防火墙,请允许这些端口
firewalld:
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
iptables:
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
iptables -I INPUT -p tcp --dport 80 -j ACCEPT


安装LetsEncrypt SSL并为虚拟主机启用HTTPS和HTTP / 2



• 安装LetsEncrypt“certbot”
yum install -y certbot-nginx

• 现在运行certbot,它将询问webroot文件夹和您的电子邮件地址。 我们安装的“certbot-nginx”应该自动修改你的NGINX配置。
certbot --authenticator webroot --installer nginx

• 创建crontab条目以每月续订SSL
crontab -e
Add:
35 4 * * 1 certbot renew >> /var/log/certbot-renew.log

• 成品!

這篇文章有幫助嗎? 135 Users Found This Useful (363 Votes)