Ubuntu-中安装Nginx+php-fpm填坑日记

Ubuntu-中安装Nginx+php-fpm填坑日记

自有服役15年的联想笔记本,安装Ubuntu-server供自己学习,开始安装Apache2作为Web服务器,最后想换成Nginx+PHP,真踩了不少坑,作填坑日记以备后续

一、安装Nginx

1
sudo apt install nginx

1、配置目录:

一般只修改/etc/nginx/nginx.conf和/etc/nginx/sites-enabled/default

1
2
3
4
5
6
/etc/nginx

/usr/sbin/nginx
/usr/lib/nginx
/usr/share/nginx
/usr/share/man/man8/nginx.8.gz

最常修改的是 /etc/nginx/sites-enabled/default

2022年6月记录,再次踩坑:

坑:升级nginx1.20.6后不再解析PHP,想办法也无济于事。最后是因为默认配置文件路径已改,php-fpm.sock不见了。
新版nginx(我安装Nginx1.20.6)默认监听:listebn:127.0.0.1:9000,所以在/run/php/下不再不再生成php7.4-fpm.sock(php-fpm.sock)
因此如果需要Nginx里的配置有链接tmp/php-fpm.sock的话,需要将listen的地址配置成和nginx的配置文件一致,同时保证这个路径已经存在,这样在启动./php-fpm的时候,会在对应路径上自动生成php-fpm.sock,
重启服务后问题解决。
另:nginx调用PHP时会有权限错误,需在nginx.conf中将用户与php-fpm.conf使用相同:user : www-data;

附:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

2、Nginx修改默认端口,找到server{}段,修改listen后的监听端口

这里不能和Apache2的端口相重合,不然是启动不了服务的,可先停用Apache2
同时,我有修改配置文件进行备份的习惯,于是备份了/etc/nginx/sitees-enabled/default-bak,然后修改端口为81,既不重合,也不占用,但就是启动不了,翻了无数网友的解决办法依然没有解决,最后看到/etc/nginx/nginx.conf 中有 include /etc/nginx/sites-enabled/*; 一项,终于明白,载入配置文件时将所有/sites-enabled/下的配置全部载入,两个配置文件冲突,肯定是启动不了的,于是将备份文件移出,启动成功。。。。。。。:)

1
2
3
4
5
vi /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
......

3、启动服务

1
sudo service nginx start	

4、重载Nginx服务,注:重载服务需要在启动的情况下才可重载

1
/etc/init.d/nginx reload

5、修改默认网站目录

1
2
3
vi /etc/nginx/sites-enabled/default
#root /var/www/html;
root /usr/share/nginx/html; # 修改默认网站目录

6、修改默认主页文档:

修改或者替换index.html

1
2
3
vi /etc/nginx/sites-enabled/default
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

二、配置PHP解析

1、安装php-fpm

1
sudo apt install php-fpm

2、配置PHP解析

我在这里配置卡了一整天,网上说的不清不楚的。

主要是找到default 配置文件内的server段内,将以下段找到,并参考将第一、二、五(七)行解除注释既可,最开始网上的教程均是要把“fastcgi_pass 127.0.0.1:9000”来解析,既是通过IP用本机解析,但不行,卡住好久,最后通过查文档,理解后才明白,用本机的“fastcgi_pass unix:/run/php/php7.3-fpm.sock;”直接解析,这样直接在系统内部就解析完成,理论上效率更高,速度更快,但一般体验不到差别。

1
2
3
4
5
6
7
8
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

3、PHP测试

在/var/www/html/中创建一个index.php的文件,内容如下:

1
2
3
<?php 
phpinfo();
?>

通过浏览器访问,你可以看到php的一个大致的信息,如果没有信息,出现下载PHP文件,则说明一些配置错误,未解析成功需要重新检查。

三、卸载重装

Ubuntu下正确卸载Nginx 然后进行重新安装

1、删除nginx,–purge包括配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sudo apt-get --purge remove nginx
#自动移除全部不使用的软件包
sudo apt-get autoremove
#列出与nginx相关的软件 并删除显示的软件
dpkg --get-selections|grep nginx
sudo apt-get --purge remove nginx
sudo apt-get --purge remove nginx-common
sudo apt-get --purge remove nginx-core
#再次执行
dpkg --get-selections|grep nginx
which nginx

#不在显示nginx
#这样就可以完全卸载掉nginx包括配置文件
#注意点:首先需要停止nginx的服务

sudo service nginx stop

四、注册系统服务

一般安装之后会自动 注册到系统服务,可通过systemd管理
/etc/systemd/system/nginx.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

nginx的几个服务几个服务命令:

1
2
3
4
5
6
7
8
9
10
/etc/init.d/nginx start  #启动
/etc/init.d/nginx stop #关闭
/etc/init.d/nginx restart #重启

sudo systemctl start
sudo systemctl stop
sudo systemctl restart
sudo systemctl enable
sudo systemctl disable
sudo systemctl status

五、反向代理配置

1
2
3
Alice->Bob: Hello Bob, how are you?
Note right of Bob: Bob thinks
Bob-->Alice: I am good thanks!

一个漂亮的关键配置(default):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
server {
listen 80; #这是监听端口
server_name localhost; #服务器名,或者域名,如有多个域名,域名之间空格,则多个域名均可访问
access_log /var/log/nginx/localhost.access.log;

## Default location
location / {
root /var/www; #网站目录
index index.php; #配置索引页
}

# Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
expires 30d;
root /var/www;
}

# Parse all .php file in the /var/www directory
location ~ .php$ { #php解析
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}

## Disable viewing .htaccess & .htpassword
location ~ /.ht {
deny all;
}
}upstream backend { server 127.0.0.1:9000;}

六、手动编译安装

安装编译环境:

1
2
3
4
5
6
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libtool
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install libssl-dev

编译安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
./configure --prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_v2_module \
--with-http_sub_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre --with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-http_dav_module

sudo make && sudo make install

七、nginx、PHP问题汇总

2023.03.31:
服务器系统重装,安装php后无法解析,最后在conf.d/default.conf 内php段fastcgi_pass unix:/run/php/php8.1-fpm.sock;前加入:

1
include        fastcgi_params;

解决


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com

×

喜欢就点赞,疼爱就打赏