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