-
安裝nginx需要的linux環(huán)境:
yum install gcc-c++ -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
-
下載nginx
cd /opt
wget [http://nginx.org/download/nginx-1.10.3.tar.gz](http://nginx.org/download/nginx-1.10.3.tar.gz)
tar xvf nginx-1.10.3.tar.gz
-
編譯安裝
cd nginx-1.10.3
# /home/service/nginx 作為nginx安裝目錄
./configure --prefix=/home/service/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-stream
make && make install
-
使用nginx代理 ,修改配置文件
vi /home/service/nginx/conf/nginx.conf
在http的{}外部添加以下內(nèi)容,例如:
stream {
server {
listen 9001;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass 172.51.20.180:9001;
}
- 啟動(dòng)nginx
先檢查配置文件是否有錯(cuò)誤: /home/service/nginx/sbin/nginx -t
啟動(dòng)nginx: /home/service/nginx/sbin/nginx -c /home/service/nginx/conf/nginx.conf
-
重載配置文件
/home/service/nginx/sbin/nginx -s reload
-
設(shè)置為開啟自啟動(dòng)
vim /usr/lib/systemd/system/nginx.service # 內(nèi)容如下
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/home/service/nginx/sbin/nginx
ExecReload=/home/service/nginx/sbin/nginx -s reload
ExecStop=/home/service/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
設(shè)置開機(jī)自啟動(dòng)
systemctl enable nginx.service
查看nginx狀態(tài)
systemctl status nginx.service
顯示Active: inactive (dead),說明nginx已經(jīng)被啟動(dòng)了,殺死進(jìn)程: pkill -9 nginx
重啟: systemctl start nginx
[Unit]:服務(wù)的說明
Description:描述服務(wù)
After:描述服務(wù)類別
[Service]服務(wù)運(yùn)行參數(shù)的設(shè)置
Type=forking是后臺(tái)運(yùn)行的形式
ExecStart為服務(wù)的具體運(yùn)行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務(wù)分配獨(dú)立的臨時(shí)空間
注意:[Service]的啟動(dòng)、重啟、停止命令全部要求使用絕對(duì)路徑
[Install]運(yùn)行級(jí)別下服務(wù)安裝的相關(guān)設(shè)置,可設(shè)置為多用戶,即系統(tǒng)運(yùn)行級(jí)別為3