2022年9月1日星期四

Ubuntu升级或安装Nginx最新稳定版(官方源安装)

卸载旧版本:

#卸载软件
sudo apt purge nginx   

#自动清理一些程序   
sudo apt autoremove nginx      

 #删除不需要的配置文件

sudo apt autoclean
dpkg -l |grep ^rc|awk ‘{print $2}’ |sudo xargs dpkg -P nginx     

#更新本地缓存文件及系统

sudo apt update && sudo apt upgrade && sudo apt dist-upgrade      

需要添加Nginx官方源才能下载Nginx的最新稳定版本

添加nginx_signing.key:
获取nginx_signing.key文件,
sudo wget http://nginx.org/keys/nginx_signing.key

sudo wget https://mirrors.nju.edu.cn/nginx/keys/nginx_signing.key
添加,
sudo apt-key add nginx_signing.key
删除nginx_signing.key,
rm -f nginx_signing.key

在 /etc/apt/sources.list.d 文件夹中新建nginx.list文件写入下面对应的两行:
jammy 为操作系统版本代号,要根据系统来选择 , 详见
查看操作系统版本命令:
lsb_release -c
官方源:
Ubuntu:
deb https://nginx.org/packages/ubuntu/ jammy nginx
deb-src https://nginx.org/packages/ubuntu/ jammy nginx

debian:
deb https://nginx.org/packages/debian/ bullseye nginx
deb-src https://nginx.org/packages/debian/ bullseye nginx

国内源:
Ubuntu22.04:
deb https://mirrors.nju.edu.cn/nginx/ubuntu/ jammy nginx
deb-src https://mirrors.nju.edu.cn/nginx/ubuntu/ nginx
debian11:
deb https://mirrors.nju.edu.cn/nginx/debian/ bullseye nginx
deb-src https://mirrors.nju.edu.cn/nginx/debian/ bullseye nginx
或debian12:
deb https://mirrors.nju.edu.cn/nginx/debian/ bookworm nginx
deb-src https://mirrors.nju.edu.cn/nginx/debian/ bookworm nginx

最后输入以下命令进行安装:
sudo apt update
如有以下报错,
W: https://nginx.org/packages/ubuntu/dists/jammy/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.

执行以下命令后再次更新源:
sudo cp /etc/apt/trusted.gpg /etc/apt/trusted.gpg.d

开始安装 nginx
sudo apt install nginx

输入nginx -v查看安装版本:

新版nginx的/etc/nginx/下的目录结构:

/etc/nginx
├── conf.d
│ ├── default.conf
├── fastcgi_params
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params

主要配置文件存放于/etc/nginx/conf.d 目录

重新加载配置并重启

nginx -t #测试配置语法
nginx -s reload #重新加载配置
sudo systemctl restart nginx #重启Nginx

nginx.pid” failed 修复:

/usr/sbin/nginx -c /etc/nginx/nginx.conf

安装PHP支持,Nginx不具有处理PHP文件的内置支持。将使用PHP-FPM(“ fastCGI进程管理器”)来处理PHP文件。

sudo apt install php-fpm

安装完成后,FPM服务将自动启动。要检查服务状态,请运行

systemctl start php7.4-fpm
systemctl status php7.4-fpm
systemctl start php8.1-fpm
systemctl status php8.1-fpm

或使用/etc/init.d 启动:
{start|stop|status|restart|reload|force-reload}
/etc/init.d/php8.1-fpm start
/etc/init.d/php8.1-fpm restart

默认静态配置

server {
listen 80;
server_name localhost;
location / {
root /var/www/html/;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/;
}
}

默认PHP配置

server {
listen 80;
server_name localhost;
# 启用HTTPS
# listen 2443 ssl;
# ssl_protocols TLSv1.3;
# ssl_certificate /etc/ssl/zs/fullchain.crt;
# ssl_certificate_key /etc/ssl/zs/private.pem;
location / {
root /var/www/html/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

TCP/UDP端口转发:
/etc/nginx/nginx.conf文件末尾添加配置代码:

stream {
include /etc/nginx/conf.d/*.stream;
}

conf.d目录下,添加*.stream后缀的配置文件,server

#TCP示例:
server {
listen 90;
listen [::]:90;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass localhost:89;
}

#UDP示例:
server {
listen 90 udp reuseport;
listen [::]:90 udp reuseport;
proxy_timeout 20s;
proxy_pass localhost:89;
}

-