Nginx 是一个流行的免费开源 Web 服务器,也可以用作反向代理、负载均衡器、邮件代理和 HTTP 缓存。用户可以运行命令sudo apt install nginx-full从 Ubuntu 系统存储库安装它,但该存储库始终是旧的。只有1.18,截止到目前本文写作的时候,最新的稳定版本是1.26
对于最新版本,有两种方法安装 Web 服务器。除了从源代码构建之外,它们还包括Ubuntu PPA和Nginx 的官方存储库。
从 Ubuntu PPA 安装 Nginx
Ondřej Surý 是Debian 开发团队的成员,维护着非常流行的 PPA,其中包含适用于 Ubuntu 22.04 和 Ubuntu 20.04 的 Nginx 的最新主线和稳定版本。
PPA 是非官方的,但包含大多数模块,并且可以从 Ubuntu 存储库中的库存版本无缝升级。并且,到目前为止它支持amd64(AMD/Intel)、arm64/armhf(例如Raspberry Pi)和ppc64el(IBM POWER 平台)设备。
.首先,按Ctrl+Alt+T打开终端,或连接到远程 Ubuntu 服务器。
然后,运行命令以确保您拥有管理软件存储库的工具:
sudo apt install software-properties-common
要添加Nginx Stable PPA,请运行命令:
sudo add-apt-repository ppa:ondrej/nginx
或者,通过命令 添加Nginx Mainline PPA :
sudo add-apt-repository ppa:ondrej/nginx-mainline
Ubuntu 在添加 PPA 后应该自动刷新包缓存。以防万一,您可以运行以下命令手动执行此操作:
sudo apt update
3.最后,运行命令安装(或从系统版本升级)nginx以及一些常用模块: 你可以先检查可安装的版本和默认版本
apt list nginx
然后执行安装命令安装
sudo apt install nginx
该命令将保留一些其他模块未安装,您可以键入以下命令,然后按 Tab键列出所有可用模块:
sudo apt install libnginx-mod-
选项 2:从官方存储库安装 Nginx
http://nginx.org/en/linux_packages.html#Ubuntu
安装先决条件:
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
导入官方 nginx 签名密钥,以便 apt 可以验证包的真实性。获取密钥:
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
验证下载的文件是否包含正确的密钥:
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
输出应包含完整指纹, 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 如下所示:
pub rsa2048 2011-08-19 [SC] [过期: 2024-06-14]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid nginx 签名密钥 <[email protected]>
如果指纹不同,请删除该文件。 这个指纹会变化请参考nginx官网. 要为稳定的 nginx 软件包设置 apt 存储库,请运行以下命令:
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
如果您想使用最新的 nginx 软件包,请运行以下命令:
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
设置存储库固定以优先选择我们的包而不是发行版提供的包:
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
要安装 nginx,请运行以下命令:
sudo apt update
sudo apt install nginx
3 配置nginx
注意:Nginx 软件包是使用不同的用户和组构建的. 根据你安装方式的不同,nginx 是使用不同的用户和组 目录的 有www-data nginx 用户和组以及/usr/share/nginx目录。 也有的存储库包用于nginx用户和组以及/etc/nginx 目录, 可以到你的nginx.conf 文件中查看 nginx 的配置除了参考官网,推荐一个工具nginxconfig.io,是个github 开源项目,由digitalocean管理,可以参考,根据自己的请求选择。
这个是我的nginx.conf配置,仅供参考:
user www-data;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
log_not_found off;
types_hash_max_size 2048;
types_hash_bucket_size 64;
client_max_body_size 16M;
# SSL
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
resolver_timeout 2s;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
分域名配置:
server {
listen 80;
root /var/www/html/wordpress;
index index.php index.html index.htm;
server_name wordpress.example.com;
client_max_body_size 500M;
# index.php fallback
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
# 404
try_files $fastcgi_script_name =404;
}
}
一般配置
# favicon.ico
location = /favicon.ico {
log_not_found off;
}
# robots.txt 禁止爬虫错误日志
location = /robots.txt {
log_not_found off;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 7d;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
add_header Access-Control-Allow-Origin "*";
expires 7d;
}
# gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
安全配置
# security headers
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: ws: wss: data: blob: 'unsafe-inline'; frame-ancestors 'self';" always;
add_header Permissions-Policy "interest-cohort=()" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# . files
location ~ /\.(?!well-known) {
deny all;
}
关注我获取更多资讯

