CentOS7安裝 Nginx + php7 + php-fpm

原文 : https://ivanagyro.medium.com/於centos7安裝-nginx-php7-php-fpm-laravel5-6-df8631681acf
安装nginx

 1/ 用yum安裝Nginx

yum install nginx

 預設開機開啟Nginx

systemctl enable nginx

 啟動Nginx

systemctl start nginx


安装php
2 安装Remi和EPEL数据源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh
https://rpms.remirepo.net/enterprise/remi-release-7.rpm

3. 启用Remi

vi /etc/yum.repos.d/remi.repo

4.更改[remi]区域的代码块为如下,相同则不用更改

[remi]
name=Remi's RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/remi/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/remi/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

5.启用Remi PHP7.3数据源

vi /etc/yum.repos.d/remi-php73.repo

6.更改[remi-php73]区域的代码块为如下

[remi-php73]
name=Remi's PHP 7.3 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php73/$basearch/
mirrorlist=https://rpms.remirepo.net/enterprise/7/php73/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/php73/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

7.升级php到php7.3

yum -y upgrade php*

8.检查php版本

php -v

9.重启php-fpm

service restart php-fpm

安裝與設定php-fpm

  1. 安裝php-fpm
yum install php-fpm

2. 設定php-fpm,修改 /etc/php-fpm.d/www.conf

修改user和group,為nginx

user = nginx
group = nginx

如果要改為使用socket file(預設使用127.0.0.1:9000),則需開啟並修改listen設定為下方樣子

listen = /path/to/unix/socketlisten.owner = nobody
listen.group = nobody
listen.mode = 0666

3. 如果在debug階段,想要開啟display error,則修改以下設定

php_flag[display_errors] = on
php_flag[display_startup_errors] = on

4. 預設開機開啟php-fpm

systemctl enable php-fpm

5. 啟動php-fpm

systemctl start php-fpm

設定Nginx

  1. 修改 /etc/nginx/conf.d/default.conf ,以連接php-fpm,以下為範例
server {
listen 80;
server_name localhost; #charset koi8-r;
#access_log /var/log/nginx/host.access.log main; root /usr/share/nginx/html;
index index.html index.htm index.php; location / {
# autoindex on; // 如果想要開啟顯示資夾目錄的話加入這條
// 建議只在debug階段開啟
try_files $uri $uri/ /index.php?$query_string;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000
// 若使用file socket則改成下面這行
# fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

2. 重新載入Nginx的設定

nginx -s reload

現在可以試著在Nginx的root directory加入index.php,內容如下。看是否能透過瀏覽器取得php的資訊

 
posted @ 2022-11-18 15:12  迷失在路上  阅读(204)  评论(0编辑  收藏  举报