Nginx + PHP7.4 安装


Nginx 安装

#如果用不到 httpd 先删除和原来的php
yum remove httpd* php*  

yum install yum-utils

#打开官方网站告诉了安装方法
#https://nginx.org/en/linux_packages.html

#如果不想看官网如何安装,请按照下面步骤安装,注意下面步骤也是从官方摘抄过来的

echo '[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true' >/etc/yum.repos.d/nginx.repo

#默认情况下nginx 只安装稳定的版本
yum install nginx

php 7.4 安装

yum install epel-release yum-utils -y
#
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
#列出可以用的php版本
yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64
#
yum-config-manager --enable remi-php74
#
yum install php php-fpm php-mysql php-cli php-devel php-gd php-pecl-memcache php-pspell php-snmp php-xmlrpc php-xml php-pdo php-pgsql php-pecl-redis php-soap php-mbstring php-opcache php-json php-cli php-zip
#

php 7.4 修改配置

#修改时区
sed -i 's@;date.timezone =@date.timezone = Asia/Shanghai@g' /etc/php.ini


#将 php 运行的 user 和 group 由 apache 修改为 nginx
sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf
sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf

#将 php-fpm 调整问文件运行
sed -i 's@listen = 127.0.0.1:9000@listen = /var/run/php-fpm/php-fpm.sock@g' /etc/php-fpm.d/www.conf

#配置监控用户和组
sed -i 's/;listen.owner = nobody/listen.owner = nginx/g' /etc/php-fpm.d/www.conf
sed -i 's/;listen.group = nobody/listen.group = nginx/g' /etc/php-fpm.d/www.conf
sed -i 's/;listen.mode = 0660/listen.mode = 0660/g' /etc/php-fpm.d/www.conf

#用下面命令将 /etc/php.ini 将;cgi.fix_pathinfo=1修改为cgi.fix_pathinfo=0
sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php.ini

Nginx 网站配置

# Nginx 配置网站
vi /etc/nginx/conf.d/default.conf
#----------------------------覆盖为如下内容----------------------------
server {
    listen       80;
    server_name  your_web_domain_or_ip_address;
    charset utf-8;

    root   /var/www/html/your_website;
    index index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }
    error_page   403  /error403.html;
    location = /error403.html {
         root /usr/share/nginx/html;
    }
}
#-----------------------------------------------------------

启动 nginx

#修改权限
chown -R root:nginx /var/lib/php
chown -R nginx: /var/www/html/your_website

#启动 php 和 nginx
systemctl restart php-fpm nginx

#新建不同的虚拟主机可以在/etc/nginx/conf.d/ 复制default.conf 为其他conf 然后修改配置