参考来源:
https://help.aliyun.com/document_detail/464753.html
https://www.cnblogs.com/laosan007/p/12803287.html
https://blog.csdn.net/qq_55364077/article/details/132207083 【安装同版本7.2的php】
需要知道对应php和nginx的安装版本
需要安装php-fpm的内容,
一、通过apt软件包管理器安装Nginx和PHP环境
sudo apt-get install php7.2-mysql php7.2-fpm php7.2-curl php7.2-xml php7.2-gd php7.2-mbstring php-memcached php7.2-zip
1.1 查看安装版本信息
root@ub1804:/etc/nginx# nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
root@ub1804:/etc/nginx# php -v
PHP 7.2.24-0ubuntu0.18.04.17 (cli) (built: Feb 23 2023 13:29:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.17, Copyright (c) 1999-2018, by Zend Technologies
修改Nginx配置文件使得支持PHP,要点:两个配置项的修改index ,location
方式一,在原配置文件上进行修改
sudo vim /etc/nginx/sites-enabled/default
在server{}内,找到index开头的配置行,在该行中添加index.php
在server{}内找到location ~ .php$ {},去除以下配置行的注释符号。(注意php7.2-fpm字段要和安装的php一致)
– 新加 index.php
## Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html ;
补充说明:
注意访问首页过程中的页面查找顺序
有时,访问站点时的URI是/,这时一般是返回网站的首页,而这与root和alias都不同。这里用ngx_http_index_module模块提供的index配置实现。
index后可以跟多个文件参数,Nginx将会按照顺序来访问这些文件,接收到请求后,Nginx首先会尝试访问path/index.php文件,如果可以访问,就直接返回文件内容结束请求,否则再试图返回path/html/index.php文件的内容,依此类推。
– 打开注释
– location ~ .php$
– include snippets/fastcgi-php.conf;
– fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; location ~ .php$
– (要和 /etc/php/7.2/fpm/pool.d 中的 www.conf 中的 listen = /run/php/php7.2-fpm.sock 一致)
在default配置文件的 网站目录路径/var/www/html中,添加index.php页面,页面内容如下
```bash
root@ub1804:/var/www/html# cat index.php
<?php
echo phpinfo();
?>
修改完nginx配置后,通过nginx -t检查配置文件有无语法问题,通过systemctl reload nginx重新加载配置
root@ub1804:/etc/nginx/sites-available# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@ub1804:/etc/nginx/sites-available# systemctl reload nginx
查看php服务和nginx的运行状态
root@ub1804:/etc/nginx/sites-available# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2024-06-24 12:25:11 CST; 5min ago
Docs: man:nginx(8)
Process: 1621 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, statu
Process: 1362 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exit
Main PID: 1622 (nginx)
Tasks: 5 (limit: 4605)
CGroup: /system.slice/nginx.service
├─1622 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─1625 nginx: worker process
├─1626 nginx: worker process
├─1627 nginx: worker process
└─1629 nginx: worker process
6月 24 12:25:04 ub1804 systemd[1]: Starting A high performance web server and a reverse proxy
6月 24 12:25:11 ub1804 systemd[1]: Started A high performance web server and a reverse proxy s
root@ub1804:/etc/nginx/sites-available# systemctl status php7.2-fpm
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2024-06-24 12:25:12 CST; 5min ago
Docs: man:php-fpm7.2(8)
Main PID: 1358 (php-fpm7.2)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 4605)
CGroup: /system.slice/php7.2-fpm.service
├─1358 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
├─1648 php-fpm: pool www
└─1649 php-fpm: pool www
6月 24 12:25:04 ub1804 systemd[1]: Starting The PHP 7.2 FastCGI Process Manager...
6月 24 12:25:12 ub1804 systemd[1]: Started The PHP 7.2 FastCGI Process Manager.
此时使用的是默认端口80,配置默认首页为index.php,使用浏览器访问,成功状态如下:
方式二,重新配置站点文件方式修改
01.创建站点目录,创建首页内容如下
root@ub1804:/var/www/example.com# pwd
/var/www/example.com
root@ub1804:/var/www/example.com# cat index.php
<?php
phpinfo();
?>
02.编辑nginx的站点配置文件
在default配置文件相同目录下创建示例站点配置文件(一台设备上多个配置文件中的server中的端口不能相同)
root@ub1804:/etc/nginx/sites-available# tree
.
├── default
└── example.com
0 directories, 2 files
root@ub1804:/etc/nginx/sites-available# cat example.com
server {
listen 8800;
server_name example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
root /var/www/example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
##END提示,>O<