centos7下安裝Nginx(yum方式和原始碼方式)
一、yum安裝
1.預設情況Centos7中無Nginx的源,最近發現Nginx官網提供了Centos的源地址。因此可以如下執行命令新增源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#檢視源是否新增成功
yum search nginx
2、安裝Nginx
yum install -y nginx
3.啟動Nginx並設定開機自動執行
systemctl start nginx.service
systemctl enable nginx.service
以下是Nginx的預設路徑:
- (1) Nginx配置路徑:/etc/nginx/
- (2) PID目錄:/var/run/nginx.pid
- (3) 錯誤日誌:/var/log/nginx/error.log
- (4) 訪問日誌:/var/log/nginx/access.log
- (5) 預設站點目錄:/usr/share/nginx/html
事實上,只需知道Nginx配置路徑,其他路徑均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查詢到。
二、原始碼安裝
#建立使用者、組
groupadd nginx
useradd nginx -M -s /sbin/nologin -g nginx
依賴包需要先安裝
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
編譯引數設定
./configure
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--http-client-body-temp-path=/usr/local/nginx/tmp/client \
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi \
--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/tmp/scgi \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-threads \
--with-stream \
--with-stream_ssl_module \
編譯和安裝
#編譯和安裝
make
make install
檢查一下配置
#測試一下配置檔案
/usr/local/nginx/sbin/nginx -t
設定擁有者和操作許可權
#設定所有者
chown -R nginx:nginx nginx
#設定操作許可權
chmod -R 755 nginx
設定開機啟動
vim /usr/lib/systemd/system/nginx.service
然後進行編輯
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
User=nginx
Group=nginx
PrivateTmp=true
[Install]
WantedBy=multi-user.target
編輯儲存完後
# 重新載入配置檔案
systemctl daemon-reload
##【驗證下服務能否配置成功】
# 啟動服務
systemctl start nginx
# 關閉服務
systemctl stop nginx
# 重啟服務
systemctl restart nginx
# 顯示服務的狀態
systemctl status nginx
#設定允許開機自啟動
systemctl enable nginx
#開放埠
firewall-cmd --zone=public --add-port=80/tcp --permanent
#重新載入
firewall-cmd --reload
三、解決非root使用者不能啟動nginx的問題
配置好了後,我們通過以下的命令就可以啟動nginx了
#啟動
/usr/local/nginx/sbin/nginx.sh
這樣倒是能啟動成功,啟動後使用ps -ef|grep nginx
檢視一下,master程序是由root啟動的,而worker程序是由我們指定的nginx使用者執行的。
但實際上我們通常用使用systemctl start nginx
來啟動,這時就會報錯了,原因就是我們使用非root使用者啟動。nginx預設使用80埠,但linux規定了root使用者可以使用1024以內的埠
,而普通使用者只能用1024以上的埠
。如果使用普通使用者來啟動nginx,會報如下的錯
nginx:[warn] the "user" directive makes sense only if the master proce***uns with super-user privileges
解決方法有以下幾種(參考部落格:Linux非root使用者程式使用小於1024埠)
1.SetUID
chmod u+s /usr/local/nginx/sbin/nginx
其實就是賦予這個nignx二進位制檔案,能夠像在root下執行
優點是,方便簡單,缺點是既然sudo許可權都不給了。這個set uid 最後也是讓nginx執行在root許可權下。 ps -ef |grep nginx 檢視的時候,nginx的主程序是執行在root下的。 雖然是可以讓普通使用者執行nginx服務,但是不是所有nginx程序都在使用者本身下執行
2.埠轉發
把監聽埠修改成1024以上的埠,然後進行埠轉發,把80埠的包轉發到1024以上 我們自定義的埠
server {
# listen 80;
# 改為監聽8088埠
listen 8088;
# 將80埠的流量轉發至8088埠
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8088 --permanent
# 開放8088埠
firewall-cmd --zone=public --add-port=8088/tcp --permanent
##其它命令
#取消80到8088埠的轉發
firewall-cmd --zone=public --remove-forward-port=port=80:proto=tcp:toport=8088 --permanent
#新增80到8088埠的轉發(帶ip)
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toaddr=xxx.xx.xx.xxx:toport=8088 --permanent
#取消80到8088埠的轉發(帶ip)
firewall-cmd --zone=public --remove-forward-port=port=80:proto=tcp:toaddr=xxx.xx.xx.xxx:toport=8088 --permanent
優點:可以用第三方使用者直接啟動,nginx的主程序就是使用者本身來啟動的。缺點,額外增加開銷,負載低的情況可以,負載高了 就不太好了
3.nginx核心超過2.1版本以後出現了能力的說法。
我們可以給/usr/local/nginx/sbin/nginx 賦予監聽80埠的許可權能力
setcap cap_net_bind_service =eip /usr/local/nginx/sbin/nginx
這樣就可以直接用普通使用者啟用nginx了。在高負載下可減少埠轉發產生的負載開銷。