本文環境為:virtualbox+centos7.3+nginx
一. nginx安裝
1 nginx安裝環境
nginx是C語言開發,建議在linux上運行,本教程使用Centos7.3作為安裝環境。
n gcc
安裝nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:
$ yum install gcc-c++ -y
n PCRE
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
$ yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。
n zlib
zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
$ yum install -y zlib zlib-devel
n openssl
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,并提供豐富的應用程序供測試或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
$ yum install -y openssl openssl-devel
2 編譯安裝
將nginx-1.8.0.tar.gz拷貝至linux服務器。
解壓:
$ tar -zxvf nginx-1.8.0.tar.gz
$ cd nginx-1.8.0
1、 configure
./configure --help查詢詳細參數(參考本教程附錄部分:nginx編譯參數)
參數設置如下:
./configure
--prefix=/usr/local/nginx
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--with-http_gzip_static_module
--with-http_ssl_module
--http-client-body-temp-path=/var/temp/nginx/client
--http-proxy-temp-path=/var/temp/nginx/proxy
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi
--http-scgi-temp-path=/var/temp/nginx/scgi
--add-module=/opt/fastdfs-nginx-module/src/
注意:上邊將臨時文件目錄指定為/var/temp/nginx,需要在/var下創建temp及nginx目錄
2、 編譯安裝
$ make
$ make install
安裝成功查看安裝目錄 :
3 啟動nginx
$ cd /usr/local/nginx/sbin/
$ ./nginx
查詢nginx進程:
15098是nginx主進程的進程id,15099是nginx工作進程的進程id
注意:執行./nginx啟動nginx,這里可以-c指定加載的nginx配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在啟動時默認加載conf/nginx.conf文件,此文件的地址也可以在編譯安裝nginx時指定./configure的參數(--conf-path= 指向配置文件(nginx.conf))
4 停止nginx
- 方式1,快速停止:
$ cd /usr/local/nginx/sbin
$ ./nginx -s stop
此方式相當于先查出nginx進程id再使用kill命令強制殺掉進程。
- 方式2,完整停止(建議使用):
$ cd /usr/local/nginx/sbin
$ ./nginx -s quit
此方式停止步驟是待nginx進程處理任務完畢進行停止。
5 重啟nginx
- 方式1,先停止再啟動(建議使用):
對nginx進行重啟相當于先停止nginx再啟動nginx,即先執行停止命令再執行啟動命令。
如下:
./nginx -s quit
./nginx
下面開始開啟iptables
二. 啟動iptables
1.安裝iptable iptable-service
#先檢查是否安裝了iptables
$ service iptables status
#安裝iptables
$ yum install -y iptables
#升級iptables
$ yum update iptables
#安裝iptables.services
$ yum install iptables.services
2.禁用/停止自帶的firewalld服務
#停止firewalld服務
$ systemctl stop firewalld
#禁用firewalld服務
$ systemctl mask firewalld
安裝iptables
yum search iptables
yum install iptables-services
3.設置規則,開啟80端口
開啟80端口
-A INPUT -p tcp --dport 80 -j ACCEPT
4.開啟iptables服務
#注冊iptables服務
#相當于以前的chkconfig iptables on
$ systemctl enable iptables.service
#開啟服務
$ systemctl start iptables.service
#查看狀態
$ systemctl status iptables.service
相關文章:
CentOS7安裝iptables防火墻