CentOS 7 編譯安裝nginx

安裝環境

  • 服務器:阿里雲
  • 系統:CentOS 7.2

安裝步驟

1. 安裝相關組件

yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

2. 編譯安裝nginx

# 獲取nginx。如果未安裝wget,使用 yum -y install wget
# 在任意目錄下執行即可,默認安裝/usr/local/nginx
wget https://nginx.org/download/nginx-1.18.0.tar.gz

# 解壓至當前目錄
tar -xzvf nginx-1.18.0

# 編譯,默認https沒有打開,需要添加 --with-http_ssl_module
./nginx-1.18.0/configure --with-http_ssl_module

# 安裝nginx
make && make install

# 添加軟鏈
ln -s /usr/local/nginx/sbin/nginx /usr/bin

# 啓動nginx
nginx

#設置開機啓動
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
  • 檢驗安裝是否成功
    瀏覽器輸入服務器地址,出現如下界面即表示安裝成功
    nginx啓動成功

3. 補充:添加服務並設置開機啓動

!!不能和上個方法同時使用

  • 編輯配置文件vim /usr/lib/systemd/system/nginx.service
[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=/usr/local/nginx/logs/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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 添加權限chmod +x /usr/lib/systemd/system/nginx.service
# 啓動
systemctl start nginx
# 停止
systemctl stop nginx
# 查看狀態
systemctl status nginx
# 修改配置後重新加載生效
systemctl reload nginx
# 重啓
systemctl restart nginx
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章