nginx 官方網站下載對應版本包
http://nginx.org/en/download.html
安裝Nginx依賴包
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
安裝PCRE
PCRE 作用是讓 Nginx 支持 Rewrite 功能。
yum install -y pcre pcre-devel
編譯安裝nginx
[root@localhost nginx-1.18.0]# mkdir /usr/local/nginx-1.18.0
[root@localhost opt]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost opt]# tar -zxvf nginx-1.18.0.tar.gz
[root@localhost opt]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.18.0]# make
[root@localhost nginx-1.18.0]# make install
root@localhost nginx-1.18.0]# /usr/local/nginx-1.18.0/sbin/nginx -v
nginx version: nginx/1.18.0
指定編譯安裝目錄 --prefix=/usr/local/nginx-1.18.0
監控模塊 --with-http_stub_status_module
SSL模塊 --with-http_ssl_module模塊
配置nginx.conf 文件
groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx
#-g:指定所屬的group
#-s:指定shell,因為它不需要登錄,所以用/sbin/nologin
#-M:不創建home目錄,因為它不需要登錄
cd /usr/local/nginx-1.18.0/conf/
vi nginx.conf
#指定nginx用戶和組
user nginx nginx;
#錯誤日志存放目錄
error_log logs/error.log;
#指定pid的路徑
pid logs/nginx.pid;
#日志格式(取消注釋即可)
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 logs/access.log main
配置systemctl 管理
[root@localhost nginx-1.18.0]# vim /usr/lib/systemd/system/nginx.service
添加以下內容
[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx-1.18.0/logs/nginx.pid
ExecStartPre=/usr/local/nginx-1.18.0/sbin/nginx -t -c /usr/local/nginx-1.18.0/conf/nginx.conf
ExecStart=/usr/local/nginx-1.18.0/sbin/nginx -c /usr/local/nginx-1.18.0/conf/nginx.conf
ExecReload=/usr/local/nginx-1.18.0/sbin/nginx -s reload
ExecStop=/usr/local/nginx-1.18.0/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl啟動nginx
重新加載服務文件
systemctl daemon-reload
systemctl start nginx.service
設置nginx開機自啟
systemctl enable nginx.service
不自啟
systemctl disable nginx.service
#查看所有已啟動服務
systemctl list-units --type=service