來架個網站吧-24.網站環境建置-編譯nginx

2024-02-28 來架個網站吧 linux 2023 iThome 鐵人賽 nginx

來架個網站吧-24.網站環境建置-編譯nginx

tags: 來架個網站吧 nginx

我是目錄


最近工作真的忙到不可開交,庫存已經耗盡,所以我拿筆記檔一下 XDDD

話說是筆記,不過我還是加點解說吧~

想要用 CentOS7 為底的環境,想說目前(202310)還是有很多無法完成脫離 CentOS7 穩定的環境吧!但是這個平台雖穩,但也日益老舊,許多跳鍵也漸漸不再支援了,就讓我為他在附上一點色彩吧。

version

考慮到 CentOS7 本身的 OpenSSL 已經不符合網路安全要求,因此我這邊是另外獨自從 OpenSSL 官網下載原始碼編譯。

  • nginx: 1.22.1
  • OS: CentOS7
  • openssl: 1.1.1s

安裝編譯套件

yum update -y
yum install -y gcc* openssl-devel pcre* patch git

設定參數

export compileRPMPath="/home/pollo/nginx-build" 

準備要的 module

  • sticky-module: 這部份是我參考其他人作法保留的。
  • nginx-module-vts
  • nginx_upstream_check_module
  • nginx_cookie_flag_module: 進行覆載平衡時作為黏著使用,避免登入系統後突然跳到其他AP,不過隨著Redis 等記憶體資料庫的使用,似乎也不太需要了。
  • ModSecurity: 我想把nginx 加一點 WAF 功能
mkdir -p ${compileRPMPath}
cd ${compileRPMPath}
#下載指定版本的nginx
export nginxVersion="1.22.1"
wget http://nginx.org/download/nginx-$nginxVersion.tar.gz
tar -xzf nginx-$nginxVersion.tar.gz
ln -sf nginx-$nginxVersion nginx
	
## 此版號為1.2.6 fix v0.1
wget https://github.com/PolloChang/nginx-sticky-module-ng/releases/download/v1.24.0/nginx-sticky-module-ng-v1.24.0.tar.gz
tar -zxf nginx-sticky-module-ng-v1.24.0.tar.gz

#下載nginx-module-vts
git clone https://github.com/vozlt/nginx-module-vts.git
#下載nginx_upstream_check_module
git clone https://github.com/yaoweibin/nginx_upstream_check_module
#下載nginx_cookie_flag_module
git clone https://github.com/AirisX/nginx_cookie_flag_module/

#更新nginx_upstream_check_module所需的patch
cd ${compileRPMPath}/nginx-sticky-module-ng
patch -p0 < ${compileRPMPath}/nginx_upstream_check_module/nginx-sticky-module.patch
cd ${compileRPMPath}/nginx
patch -p1 < ${compileRPMPath}/nginx_upstream_check_module/check_1.20.1+.patch


# WAF

export MODSECURITY_INC="${compileRPMPath}/ModSecurity/headers/"
git clone -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity.git
git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git

# 指定OpenSSL,特定版本請至 https://ftp.openssl.org/ 裡面尋找

export openSSLVersion="openssl-1.1.1s"
cd ${compileRPMPath}
curl --insecure -O https://www.openssl.org/source/${openSSLVersion}.tar.gz
tar zxf ${openSSLVersion}.tar.gz
ls ${compileRPMPath}/${openSSLVersion}

編譯

cd ${compileRPMPath}/nginx
./configure \
 --user=nginx \
 --group=nginx \
 --prefix=/etc/nginx \
 --sbin-path=/usr/sbin/nginx \
 --conf-path=/etc/nginx/nginx.conf \
 --pid-path=/var/run/nginx.pid \
 --lock-path=/var/run/nginx.lock \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --with-http_gzip_static_module \
 --with-http_stub_status_module \
 --with-http_v2_module \
 --with-http_ssl_module \
 --with-pcre \
 --with-file-aio \
 --with-http_realip_module \
 --without-http_scgi_module \
 --without-http_uwsgi_module \
 --without-http_fastcgi_module \
 --with-openssl=${compileRPMPath}/${openSSLVersion} \
 --add-module=${compileRPMPath}/nginx-sticky-module-ng \
 --add-module=${compileRPMPath}/nginx-module-vts \
 --add-module=${compileRPMPath}/nginx_upstream_check_module \
 --add-module=${compileRPMPath}/nginx_cookie_flag_module \
 --add-dynamic-module=${compileRPMPath}/ModSecurity-nginx
  • 編譯
make -j8 # 依CPU 核心數設定,這裡是4核心

安裝

useradd -s /sbin/nologin nginx
mkdir -p /var/log/nginx/
chown nginx:nginx /var/log/nginx/
ln -s /var/log/nginx /etc/nginx/logs

建立守護程序

  • /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s TERM $MAINPID
ExecTest=/usr/sbin/nginx -t

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload && sudo systemctl start nginx && sudo systemctl status nginx
sudo systemctl enable nginx.service
firewall-cmd --permanent --zone=public --add-service=https

打包編譯

  • .deb
checkinstall -D

自簽 https,建立可信任的自簽憑證

安全的網站https 是必備的,一開始憑證自簽即可。

mkdir /etc/nginx/ssl
  • ssl.conf
[req]
prompt = no
default_md = sha256
default_bits = 2048
distinguished_name = dn
x509_extensions = v3_req

[dn]
C = TW
ST = Taiwan
L = Taipei
O = Duotify Inc.
OU = IT Department
emailAddress = [email protected]
CN = localhost

[v3_req]
subjectAltName = @alt_names

[alt_names]
DNS.1 = *.localhost
DNS.2 = localhost
IP.1 = 10.192.1.108
IP.2 = 10.1.4.43
  • alt_names: 這一段可以增加多組的IP, domain
openssl req -x509 -new -nodes -sha256 -utf8 -days 3650 -newkey rsa:2048 -keyout /etc/nginx/ssl/server.key -out /etc/nginx/ssl/server.crt -config /etc/nginx/ssl/ssl.conf
openssl pkcs12 -export -in /etc/nginx/ssl/server.crt -inkey /etc/nginx/ssl/server.key -out /etc/nginx/ssl/server.pfx
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
  • req:使用 X.509 Certificate Signing Request(CSR) Management 產生憑證。
  • -x509:建立自行簽署的憑證。
  • -nodes:不要使用密碼保護,因為這個憑證是 NGINX 伺服器要使用的,如果設定密碼的話,會讓伺服器每次在啟動時書需要輸入密碼。
  • -days 365:設定憑證的使用期限,單位是天,如果不想時常重新產生憑證,可以設長一點。
  • -newkey rsa:2048:同時產生新的 RSA 2048 位元的金鑰。
  • -keyout:設定金鑰儲存的位置。
  • -out:設定憑證儲存的位置。
scp [email protected]:/etc/nginx/ssl/server.crt ./
  • windows
certutil -addstore -f "ROOT" server.crt
C:\Users\pollochang>certutil -addstore -f "ROOT" server.crt
ROOT "受信任的根憑證授權單位"
簽章符合公開金鑰憑證 "localhost" 已新增到存放區中。
CertUtil: -addstore 命令成功完成。               
  • linux
sudo cp server.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
❯ sudo update-ca-certificates
Updating certificates in /etc/ssl/certs...
rehash: warning: skipping ca-certificates.crt,it does not contain exactly one certificate or CRL
1 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
Processing triggers for ca-certificates-java (xxxxxxx~deb12u1) ...
Adding debian:server.pem
done.
done.

參考資料

如何使用 OpenSSL 建立開發測試用途的自簽憑證 (Self-Signed Certificate)