CentOS7 安装 Nginx & PHP
Posted 优小U
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7 安装 Nginx & PHP相关的知识,希望对你有一定的参考价值。
安装 nginx
# Nginx没有内置在默认的CentOS repositories, 我们先安装EPEL repository
yum install epel-release -y
# 安装 Nginx
yum install nginx -y
# 启动nginx和让nginx在linux启动时自动运行
systemctl start nginx
systemctl enable nginx
安装 php v7.4
# 安装额外包括 PHP v7.4 的 CentOS repo
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm
# 使 php74 repository 生效(默认不生效):
yum install yum-utils -y
yum-config-manager --enable remi-php74
# 然后安装 PHP package:
yum --enablerepo=remi,remi-php72 install php-fpm php-common
# 安装通用模块:
yum --enablerepo=remi,remi-php72 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
配置 Nginx 和 PHP 7.4 匹配
修改 /etc/nginx/conf.d/default.conf
:
server {
listen 80;
server_name your_server_ip;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \\.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
修改完重启nginx:
systemctl restart nginx
进行 PHP-FPM 的配置:/etc/php-fpm.d/www.conf
找到并替换几行:
user = apache 换为: user = nginx
group = apache 换为:group = nginx
listen.owner = nobody 换为: listen.owner = nginx
listen.group = nobody 换为: listen.group = nginx
在listen = 127.0.0.1:9000 下面增加:
listen = /var/run/php-fpm/php-fpm.sock
最后, 启动 php-fpm 并让它自启动:
systemctl start php-fpm.service
systemctl enable php-fpm.service
测试
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php
以上是关于CentOS7 安装 Nginx & PHP的主要内容,如果未能解决你的问题,请参考以下文章
安装epel 解决centos7无法使用yum安装nginx