目录
一、环境准备
1.1. 下载软件包
OpenSSL
shell
# https://github.com/openssl/openssl/releases/
wget https://github.com/openssl/openssl/releases/download/openssl-4.0.1/openssl-4.0.1.tar.gz && \
sudo tar -zxf openssl-4.0.1.tar.gz -C /usr/local/srcPcre2
shell
# https://github.com/PhilipHazel/pcre2/releases
wget -c https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.47/pcre2-10.47.tar.gz && \
sudo tar -zxf pcre2-10.47.tar.gz -C /usr/local/srcFancyIndex
shell
# https://github.com/aperezdc/ngx-fancyindex/releases
wget -c https://github.com/aperezdc/ngx-fancyindex/releases/download/v0.6.0/ngx-fancyindex-0.6.0.tar.xz && \
xz -d ngx-fancyindex-0.6.0.tar.xz && \
sudo tar -xf ngx-fancyindex-0.6.0.tar -C /usr/local/srcNJS
shell
# https://github.com/nginx/njs/tags
wget https://github.com/nginx/njs/archive/refs/tags/0.9.9.tar.gz -O njs-0.9.9.tar.gz && \
sudo tar -zxf njs-0.9.9.tar.gz -C /usr/local/srcGeoIP2
shell
# https://github.com/leev/ngx_http_geoip2_module/releases
wget -c https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz -O ngx_http_geoip2_module-3.4.tar.gz && \
sudo tar -zxf ngx_http_geoip2_module-3.4.tar.gz -C /usr/local/srcNginx
shell
# https://nginx.org/en/download.html
wget -c http://nginx.org/download/nginx-1.30.2.tar.gz && \
sudo tar -zxf nginx-1.30.2.tar.gz -C /usr/local/src1.2. 新增用户和组 (可选)
本文使用自带的 www-data 用户和组
shell
# On Ubuntu/Debian:
sudo adduser \
--system \
--shell /bin/bash \
--gecos 'Nginx web server' \
--group \
--disabled-password \
--home /var/www \
nginx
# On Fedora/RHEL/CentOS:
sudo groupadd --system nginx
sudo adduser \
--system \
--shell /bin/bash \
--comment 'Nginx web server' \
--gid nginx \
--home-dir /home/nginx \
--create-home \
nginx二、编译安装
2.1. 安装编译工具
shell
# On Ubuntu/Debian:
sudo apt -y install build-essential libzip-dev libxml2-dev libxslt-dev
shell
# On Fedora/RHEL/CentOS:
# sudo yum -y groupinstall 'Development Tools'
sudo yum install -y gcc gcc-c++ zlib-devel2.2. 安装扩展工具
安装 MaxmindDB
shell
# https://github.com/maxmind/libmaxminddb/releases
# sudo yum install libmaxminddb libmaxminddb-devel -y
sudo apt install libmaxminddb-dev mmdb-bin -y
shell
# https://github.com/P3TERX/GeoLite.mmdb
sudo mkdir -p /usr/share/geoip/
# 国内
sudo wget -c --no-check-certificate -P /usr/share/geoip \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/geoip/{GeoLite2-ASN.mmdb,GeoLite2-City.mmdb,GeoLite2-Country.mmdb}
# 国外
sudo wget -c --no-check-certificate -P /usr/share/geoip \
https://git.io/{GeoLite2-ASN.mmdb,GeoLite2-City.mmdb,GeoLite2-Country.mmdb}
shell
mmdblookup --file /usr/share/geoip/GeoLite2-Country.mmdb --ip 43.138.171.130 country names en
mmdblookup --file /usr/share/geoip/GeoLite2-City.mmdb --ip 43.138.171.130 city names en安装 Perl (可选)
shell
# https://www.cpan.org/src/
wget -c https://code.wangyan.cloud//wangyan/packages/releases/download/perl/perl-5.42.1.tar.gz
wget -c https://www.cpan.org/src/5.0/perl-5.42.1.tar.gz
tar -xzf perl-5.42.1.tar.gz -C /usr/local/src && \
cd /usr/local/src/perl-5.41.5 && \
./Configure -des -Dprefix=/usr/local/perl && \
make && make install && \
perl -v2.3. 编译安装
注意修改 openssl 、pcre 、ngx-fancyindex 、ngx_http_geoip2_module 版本号。
部分组件不需要,可以删减。
shell
cd /usr/local/src/nginx-1.30.2
shell
sudo ./configure \
--prefix=/usr/local/nginx \
--user=www-data \
--group=www-data \
--with-stream \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-openssl=/usr/local/src/openssl-4.0.1 \
--with-pcre=/usr/local/src/pcre2-10.47 \
--http-client-body-temp-path=/usr/local/nginx/temp/client_body \
--http-proxy-temp-path=/usr/local/nginx/temp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/temp/scgi \
--add-dynamic-module=/usr/local/src/ngx-fancyindex-0.6.0 \
--add-dynamic-module=/usr/local/src/ngx_http_geoip2_module-3.4 \
--add-dynamic-module=/usr/local/src/njs-0.9.9/nginx
shell
sudo make && sudo make install
shell
sudo ln -s /usr/local/nginx/conf/ /etc/nginx && \
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/ && \
sudo mv -f /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak2.5. 设置环境变量
shell
sudo vim /etc/profile
shell
PATH=$PATH:/usr/local/mysql/bin:/usr/local/php/bin:/usr/local/nginx/sbin
export PATH三、配置
3.1. 配置文件目录
缓存目录
shell
sudo mkdir -p /usr/local/nginx/temp/{client_body,proxy,fastcgi,uwsgi,scgi} && \
sudo chown -R www-data:www-data /usr/local/nginx/temp/日志目录
shell
sudo mkdir -p /var/log/nginx && \
sudo chmod 711 /var/log/nginx && \
sudo chown -R www-data:www-data /var/log/nginx配置文件目录
复制配置文件
shell
cd ~/nginx && \
sudo cp -r resources/conf/* /usr/local/nginx/conf/ && \
sudo cp -r resources/geoip /usr/share/ && \
sudo chown -R www-data:www-data /usr/share/geoip下载配置文件(可选)
shell
sudo mkdir -p /usr/local/nginx/conf/{conf.d,custom-config,error-pages,modules-available,modules-enabled,rewrite-rules,sites-available,sites-enabled,ssl-certs}
shell
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/nginx.conf
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf/conf.d \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/conf.d/default_server.conf
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf/custom-config \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/custom-config/{fancyindex.conf,general.conf,geoip.conf,ips.conf,php_fastcgi.conf,reverse-proxy.conf,security.conf}
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf/error-pages \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/error-pages/{400.html,403.html,404.html,500.html,502.html}
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf/modules-available \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/modules-available/{ngx_http_fancyindex.conf,ngx_http_geoip2_module.conf,ngx_http_js.conf,ngx_stream_geoip2.conf,ngx_stream_js.conf}
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf/rewrite-rules \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/rewrite-rules/wordpress.conf
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf/sites-available \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/sites-available/{example.com.conf,proxy.example.com.conf}
sudo wget -c --no-check-certificate -P /usr/local/nginx/conf/ssl-certs \
https://code.wangyan.cloud//wangyan/nginx/raw/branch/main/resources/conf/ssl-certs/dhparam.pem设置目录权限
shell
sudo chown 0:0 /usr/local/nginx/conf -R && \
find /usr/local/nginx/conf -type f -exec sudo chmod 644 {} \; && \
find /usr/local/nginx/conf -type d -exec sudo chmod 755 {} \;证书特殊权限
shell
sudo chmod 777 /usr/local/nginx/conf/ssl-certs && \
sudo chmod o+t /usr/local/nginx/conf/ssl-certs3.2. 开机自启动
shell
sudo vim /usr/lib/systemd/system/nginx.service
shell
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
shell
sudo chmod +x /usr/lib/systemd/system/nginx.service && \
sudo systemctl enable --now nginx.service3.3. 防火墙设置
Fedora/RHEL/CentOS:
shell
systemctl enable --now firewalld.service
sudo firewall-cmd --permanent --zone=public --add-service=http && \
sudo firewall-cmd --permanent --zone=public --add-service=https && \
sudo firewall-cmd --reloadUbuntu/Debian:
shell
sudo apt install ufw
shell
sudo vim /etc/ufw/applications.d/ufw-mysqlserver
shell
[MySQL]
title=MySQL database server
description=MySQL database server
ports=3306/tcp
shell
sudo ufw allow 'WWW Full'
sudo ufw allow 'OpenSSH'
sudo ufw allow 'MySQL'
sudo ufw enable
sudo ufw status verbose
sudo ufw delete allow 80/tcp
sudo ufw status numbered
sudo ufw delete 1