1.安裝環境
- CentOS7
- Nginx版本 nginx-1.15.8.tar.gz
2.安裝
2.1 安裝環境
2.1.1 gcc安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝,命令:
yum install gcc-c++
2.1.2 PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:
yum install -y pcre pcre-devel
2.1.3 zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
yum install -y zlib zlib-devel
2.1.4 OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel
2.2 nginx安裝包下載
一下提供兩種方式:
2.2.1 官網下載
http://nginx.org/en/download.html
下載後通過ftp工具上傳到linux上。
2.2.2 使用wget
命令下載
wget -c https://nginx.org/download/nginx-1.15.8.tar.gz
如下:
2.3 nginx安裝
2.3.1 將上面的壓縮包解壓縮,命令:
tar -zxvf nginx-1.15.8.tar.gz
解壓結束後
2.3.2 配置、編譯安裝
cd到nginx-1.15.8下 這裏使用默認配置,使用組合命令,如下:
./configure && make && make install
等待安裝完成後通過whereis nginx命令查看安裝路徑
whereis nginx
然後cd到/usr/local/nginx/sbin目錄下,通過./nginx命令開啓nginx服務
可以通過 ps -ef | grep nginx 命令來檢測系統是否開啓了nginx線程
./nginx
nginx其他命令:
#開啓nginx服務
./nginx
#待nginx進程處理任務完畢進行停止
./nginx -s quit
#相當於先查出nginx進程id再使用kill命令強制殺掉進程
./nginx -s stop
#重啓
./nginx -s reload
查看nginx是否安裝成功用curl http://127.0.0.1
curl http://127.0.0.1
如果外網訪問時,訪問不到,請繼續以下 外網訪問 步驟
3. 外網訪問
nginx搭建好後,向外拋出80端口,通過 firewall-cmd --query-port=80/tcp 來查看80端口是否開啓,每開啓時候爲:no
執行下面命令:
//允許某端口放行
firewall-cmd --permanent --add-port=3389/tcp
//需要留意的是在編寫完規則之後,要運行--reload參數
firewall-cmd --reload
成功之後頁面:
下面提供一些輔助命令:
查詢端口號80 是否開啓:firewall-cmd --query-port=80/tcp
永久開放80端口號:firewall-cmd --permanent --zone=public --add-port=80/tcp
移除80端口號:firewall-cmd --permanent --zone=public --remove-port=80/tcp
--zone #作用域
--add-port=80/tcp #添加端口,格式爲:端口/通訊協議
--permanent #永久生效,沒有此參數重啓後失效
查看防火牆狀態
systemctl status firewalld.service
啓動|關閉|重新啓動 防火牆
systemctl [start|stop|restart] firewalld.service
4. 設置開機自啓以及系統服務
centos 7以上是用Systemd進行系統初始化的,Systemd 是 Linux 系統中最新的初始化系統(init),它主要的設計目標是克服 sysvinit 固有的缺點,提高系統的啓動速度。關於Systemd的詳情介紹在https://www.ibm.com/developerworks/cn/linux/1407_liuming_init3。
這裏是用源碼編譯安裝的,所以要手動創建nginx.service服務文件。
首先進入cd /lib/systemd/
system
cd /lib/systemd/system/
然後創建nginx.service
vi nginx.service
編輯內容:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
ExecStart後面的參數爲:用戶安裝nginx目錄下的sbin下的nginx命令,前面有提到。
[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是後臺運行的形式
ExecStart爲服務的具體運行命令
ExecReload爲重啓命令
ExecStop爲停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啓動、重啓、停止命令全部要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置爲多用戶,即系統運行級別爲3
保存退出。
4.1 開機啓動命令
systemctl enable nginx.service
此命令設置後如果systemctl restart nginx.service 等命令不能使用,請重啓再試。
4.2 其他命令
#啓動nginx服務
systemctl start nginx.service
#關閉
systemctl stop nginx.service
#重啓
systemctl restart nginx.service
#設置開機自啓動
systemctl enable nginx.service
#停止開機自啓動
systemctl disable nginx.service
#查看服務當前狀態
systemctl status nginx.service
#查看所有已啓動的服務
systemctl list-units --type=service
注意:以上操作步驟,視安裝情況而定。