編譯安裝和apt安裝Nginx1.14.0


安裝依賴

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

在Ubuntu 下, 對應需要安裝的是

sudo apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev

 

建立用戶及用戶組

groupadd -r nginx
# -r 表示創建的是系統組
useradd -s /sbin/nologin -g nginx -r nginx
# -r 表示創建的是系統用戶
id nginx
# 即使用其他用戶啟動nginx, 也必須創建nginx用戶和用戶組, 否則會出現 nginx: [emerg] getpwnam("nginx") failed 錯誤

編譯和安裝

./configure --prefix=/opt/nginx/nginx-1.14.0 --user=nginx --group=nginx --with-http_gzip_static_module --with-pcre --with-http_ssl_module --with-http_flv_module --with-stream --with-stream_ssl_module --with-http_stub_status_module
make
make install

 

如果需要rtmp服務, 需要加上rtmp模塊

git clone https://github.com/arut/nginx-rtmp-module.git

./configure --prefix=/opt/nginx/nginx-1.18.0 --user=nginx --group=nginx --with-http_gzip_static_module --with-pcre --with-http_ssl_module --with-stream --with-stream_ssl_module --with-http_stub_status_module --add-module=/home/milton/backup/nginx-rtmp-module

 

建立latest目錄和軟鏈, 方便將來替換為新版本

cd /opt/nginx/
ln -s nginx-1.14.0 latest

配置

將nginx加入系統服務

cd /lib/systemd/system/
vi nginx.service

# 內容

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/opt/nginx/latest/sbin/nginx -t -c /opt/nginx/latest/conf/nginx.conf
ExecStart=/opt/nginx/latest/sbin/nginx
ExecReload=/opt/nginx/latest/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# 內容結束

systemctl enable nginx.service

打開端口啟動服務

firewall-cmd --zone=public --list-all
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload

# 啟動nginx
systemctl start nginx

Ubuntu18.04下apt安裝Nginx1.14.0

Ubuntu18.04下默認的nginx版本就是1.14.0, 可以直接通過 sudo apt install nginx 安裝. 安裝后會自動加入service並啟動, 可以通過 sudo systemctl list-unit-files 檢查

默認的配置文件位於 /etc/nginx/nginx.conf, 這是給Ubuntu定制的配置文件, 基礎配置部分, 會加載 /etc/nginx/modules-enabled/*.conf , 在 http 部分, 會自動加載 include /etc/nginx/conf.d/*.conf 和 include /etc/nginx/sites-enabled/*, 這樣方便用戶通過 modules-enabled, modules-available 和 sites-enabled, sites-available 來管理可用站點.

從這里開始配置就和編譯安裝的一樣了.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM