安裝nginx之前要先安裝依賴包
yum install -y gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel
1.gcc安裝:安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝 2.PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。 nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫 3.zlib庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。 4.OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。 nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
nginx下載與安裝
直接下載.tar.gz安裝包,下載地址:https://nginx.org/en/download.html
把下載的包放到/usr/local/nginx目錄,然后解壓安裝到nginx-1.12.0當前目錄,make編譯安裝
[root@yoyo sbin]# cd ~ [root@yoyo ~]# cd /usr/local/ [root@yoyo local]# mkdir nginx [root@yoyo local]# cd nginx

[root@yoyo nginx]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz(這個指令暫時用不了可進入網頁直接下載) [root@yoyo nginx]# tar -zxvf nginx-1.12.0.tar.gz [root@yoyo nginx]# cd nginx-1.12.0
# 安裝到當前目錄 [root@yoyo nginx]# ./configure
(記住進入到nginx-1.12.0這里再啟動不然會提示不是目錄)
(如果提示權限不足更改屬性):
chmod -R 777 ./configure

[root@yoyo nginx]# make [root@yoyo nginx]# make install 到此為止環境已經安裝好,接下來啟動nginx服務 [root@yoyo nginx]# cd /usr/local/nginx/sbin/ [root@yoyo nginx]# ./nginx

開放80端口: firewall-cmd --add-port=80/tcp --permanent firewall-cmd --add-port=80/udp --permanent firewall-cmd --reload
啟動服務后,nginx默認是在80端口啟動的,在瀏覽器輸入http:// 192.168.80.129:80/端口默認可以省略),能正常訪問到頁面,說明服務啟動成功

相關指令 先cd到/usr/local/nginx/sbin/ 1.啟動服務 ./nginx 2.停止服務,此方式停止步驟是待nginx進程處理任務完畢進行停止。 ./nginx -s stop 3.退出服務,此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。 ./nginx -s quit 4.重新加載,當 ngin x的配置文件 nginx.conf 修改后,要想讓配置生效需要重啟 nginx, 使用-s reload不用先停止 ngin x再啟動 nginx 即可將配置信息在 nginx 中生效 ./nginx -s reload 5.查詢nginx進程 ps aux|grep nginx
修改nginx啟動端口
如果80端口之前已經使用過了,可以修改nginx的服務端口,先cd到/usr/local/nginx/conf目錄
為了保險起見,編輯前先備份下原來的文件:cp nginx.conf nginx.conf.bak
vim打開后,找到服務端口listen 80這段,輸入鍵盤上i鍵后編輯,改成81

編輯完成后按Esc鍵,輸入:wq保存退出 修改后重新加載下配置文件 cd /usr/local/nginx/sbin/ ./nginx -s reload 最后,開放81端口 開放81端口: firewall-cmd --add-port=81/tcp --permanent firewall-cmd --add-port=81/udp --permanent firewall-cmd --reload
