Nginx 是一款輕量級的 Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,它是由 C 語言開發(fā),建議在 Linux 下運行。
Nginx 需要的安裝環(huán)境
如果不知道 linux/centOS 上是否安裝了下面的軟件,那么先嘗試安裝 Nginx ,如果安裝的過程中出現(xiàn)問題,那么根據(jù)錯誤提示,安裝相應(yīng)的必要環(huán)境,例如:
./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP gzip module requires the zlib library.
安裝 gcc
安裝Nginx的編譯環(huán)境gcc
yum install gcc-c++
安裝 pcre pcre-devel
nginx的http模塊使用pcre解析正則表達式,所以安裝perl兼容的正則表達式庫
yum install -y pcre pcre-devel
安裝 zlib
nginx使用zlib對http包的內(nèi)容進行g(shù)zip
yum install -y zlib zlib-devel
安裝 Open SSL
nginx不僅支持http協(xié)議,還支持https(即在ssl協(xié)議上傳輸http),如果使用了https,需要安裝OpenSSL庫
yum install -y openssl openssl-devel
Nginx 的安裝
解壓
tar -zxvf nginx-…tar.gz
配置
使用默認配置:
cd nginx-..
./configure
注意:使用默認配置時,nginx被安裝到/usr/local/nginx下。
編譯、安裝
make && make install
配置環(huán)境變量
vim /etc/profile
在合適位置添加環(huán)境變量
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
重新編譯 /etc/profile 文件
source /etc/profile
注意:重新編譯文件時,如果會出現(xiàn)下面的問題
[root@pc-server nginx]# source /etc/profile
bash: id: command not found
bash: tty: command not found
3
此時說明在添加環(huán)境變量時,有單詞寫錯了,或者是少寫了$PATH,此時需要重新修改/etc/profile文件,修改文件的命令改為
/bin/vi /etc/profile
然后斷開linux連接,再重新連接即可。
Nginx 的運行
因為將Nginx配置到了環(huán)境變量中,因此,在任何路徑下都可以直接使用nginx命令,而不需要進入nginx路徑下執(zhí)行。
啟動
啟動代碼格式:nginx安裝目錄地址 -c nginx配置文件地址
例如:
[root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
查看 nginx 進程
ps -ef | grep nginx
這里寫圖片描述
關(guān)閉
nginx -s stop
重啟
nginx -s reload
測試 nginx 配置腳本是否運行正常
通??梢酝ㄟ^這個命令查看nginx配置文件的位置
nginx -t
這里寫圖片描述
nginx 開機自啟
在rc.local文件中加入/usr/local/nginx/sbin/nginx
vim /etc/rc.local
這里寫圖片描述
nginx 指定配置文件啟動
nginx -c /usr/local/nginx/conf/nginx.conf