本章节将详细介绍从源代码构建 nginx 的完整流程,包括依赖库安装、编译选项配置、编译安装等步骤。
sudo apt update
sudo apt install build-essential
sudo yum groupinstall "Development Tools"
sudo yum install gcc gcc-c++ make
xcode-select --install
sudo apt install libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
sudo yum install pcre-devel zlib-devel openssl-devel
brew install pcre openssl
# 创建源代码目录
mkdir -p ~/src/nginx
cd ~/src/nginx
# 下载最新稳定版
wget http://nginx.org/download/nginx-1.24.0.tar.gz
# 解压
tar -xzvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=Ubuntu \
--with-debug \
--with-compat \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_perl_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip2_module \
--with-stream_ssl_preread_module
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-file-aio \
--with-http_secure_link_module \
--with-pcre=/usr/local/opt/pcre \
--with-openssl=/usr/local/opt/openssl
# 编译
make
# 安装
sudo make install
# 创建 nginx 用户
sudo useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
# 创建必要的目录
sudo mkdir -p /var/cache/nginx/client_temp /var/cache/nginx/proxy_temp /var/cache/nginx/fastcgi_temp /var/cache/nginx/uwsgi_temp /var/cache/nginx/scgi_temp
# 设置权限
sudo chown -R nginx:nginx /var/cache/nginx
sudo chmod 770 /var/cache/nginx/*
创建 /etc/systemd/system/nginx.service 文件:
[Unit]
Description=nginx - high performance web server
Documentation=https://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
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
# 重新加载 systemd
sudo systemctl daemon-reload
# 启动 nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 查看状态
sudo systemctl status nginx
# 检查版本
nginx -v
# 测试配置
nginx -t
# 查看编译参数
nginx -V
# 测试服务
curl http://localhost
/etc/nginx/nginx.conf:
user nginx;
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/default.conf:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# ufw
sudo ufw allow 'Nginx Full'
# 停止服务
sudo systemctl stop nginx
sudo systemctl disable nginx
# 删除文件
sudo rm -rf /etc/nginx
sudo rm -rf /usr/sbin/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/cache/nginx
# 删除服务
sudo rm /etc/systemd/system/nginx.service
sudo systemctl daemon-reload
# 删除用户
sudo userdel nginx
# 查看错误日志
sudo tail -f /var/log/nginx/error.log
# 查看访问日志
sudo tail -f /var/log/nginx/access.log
# 检查配置
sudo nginx -t
# 查看进程
ps aux | grep nginx
# 查看端口
sudo netstat -tlnp | grep :80