center OS7 源碼包安裝nginx

方法一:centerOS7源碼包安裝nginx

1.安裝環境

一. gcc 安裝
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:php

yum install gcc-c++html

二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:linux

yum install -y pcre pcre-develnginx

三. zlib 安裝
zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。c++

yum install -y zlib zlib-devel正則表達式

四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。算法

yum install -y openssl openssl-devel後端

. wget 安裝centos

[root@localhost ~]# yum -y install wget瀏覽器

六. 下載nginx

七. wget http://nginx.org/download/nginx-1.14.0.tar.gz

八. 解壓nginx

1).解壓縮:tar -zxvf nginx-1.14.0.tar.gz

2).進入解壓縮後文件夾:cd nginx-1.14.0

九.配置編譯參數命令:(可使用./configure --help查詢詳細參數)

./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 \

--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

注:安裝以前須要手動建立上面指定的nginx文件夾,即/var/temp、/var/temp/nginx、/var/run/nginx/文件夾,不然啓動時報錯

十.編譯並安裝

  命令:make && make install

能夠進入/usr/local/nginx查看文件是否存在conf、sbin、html文件夾,若存在則安裝成功

2.基本使用

1.啓動nginx

1).進入安裝目錄

cd /usr/local/nginx/sbin/

2).啓動

./nginx

3).若報錯:[emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)

   須要查看下是否是在/var/run文件夾下不存在nginx文件夾,不存在則新建

4).查看是否啓動:ps -ef | grep nginx

   若是有master和worker兩個進程證實啓動成功

  注意:執行./nginx啓動nginx,這裏能夠-c指定加載的nginx配置文件,以下:

./nginx -c /usr/local/nginx/conf/nginx.conf

   若是不指定-c,nginx在啓動時默認加載conf/nginx.conf文件,此文件的地址也能夠在編譯安裝nginx時指定./configure的參數(--conf-path= 指向配置文件(nginx.conf))

2.中止

1).暴利kill(不推薦使用)

kill -9 processId

2).快速中止

cd /usr/local/nginx/sbin && ./nginx -s stop

   此方式至關於先查出nginx進程id再使用kill命令強制殺掉進程

3).完整中止(建議使用)

cd /usr/local/nginx/sbin && ./nginx -s quit

   此方式中止步驟是待nginx進程處理任務完畢進行中止

3.重啓及從新加載配置

1.先中止再啓動(建議使用)

./nginx -s quit && ./nginx

2.從新加載配置文件
./nginx -s reload

 

 

 

 

 

 

方法二:CentOS7 yum 安裝 Nginx最新版本

 

1.下載對應當前系統版本的nginx包(package)


# wget  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm


2.創建nginx的yum倉庫
# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

3.下載並安裝nginx
# yum install nginx

4.啓動nginx服務
systemctl start nginx


配置
默認的配置文件在 /etc/nginx 路徑下,使用該配置已經能夠正確地運行nginx;如須要自定義,修改其下的 nginx.conf 等文件便可。
******************************************************************************************
user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

 

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;

 #設定負載均衡服務器列表

    upstream group1{

        #後端服務器訪問規則

        #ip_hash;

        #weight參數表示權重值,權值越高被分配到的概率越大

#PC_Local

        server 192.168.187.133:80 weight=5;

#PC_Server

        server 192.168.187.134:80 weight=5;   

    }

server {

listen       81; #設置對外端口

server_name  192.168.187.133 ; #設置識別請求域名

location / {

#定義服務器的默認網站根目錄位置

            #root html;

            #定義首頁索引文件的名稱

            #index index.html index.htm index.php;

proxy_pass  http://group1 ; #分流到group1集羣

}

    }

}

********************************************************************************************
測試

在瀏覽器地址欄中輸入部署nginx環境的機器的IP,若是一切正常,應該能看到以下字樣的內容。

 

啓動區域端口和協議組合

語法:

firewall-cmd [--zone=<zone>] --add-port=<port>[-<port>]/<protocol> [--timeout=<seconds>]

 

firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,沒有此參數重啓後失效) 添加80端口,nginx默認是80端口

 

從新載入
firewall-cmd --reload
查看
firewall-cmd --zone=public --query-port=80/tcp
刪除
firewall-cmd --zone=public --remove-port=80/tcp --permanent

 

+++++++++++++++++=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

傳統啓動端口區域的方法:

1.執行一下命令:

systemctl stop firewalld

systemctl mask firewalld


2.而且安裝iptables-services:

yum install iptables-services


3.設置開機啓動:

systemctl enable iptables

systemctl stop iptables

systemctl start iptables

systemctl restart iptables

systemctl reload iptables


4.保存設置:

service iptables save

OK,再試一下應該就好使了

5.開放某個端口 在/etc/sysconfig/iptables裏添加

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

 

 

Nginx拆卸

1.先輸入命令:yum remove nginx  接着屏幕上打印出如下文字:

已加載插件:fastestmirror, langpacks

正在解決依賴關係

--> 正在檢查事務

---> 軟件包 nginx.x86_64.1.1.8.0-1.el7.ngx 將被 刪除

--> 解決依賴關係完成

.........

到此,卸載nginx完畢,

2.可使用which nginx找一下是否卸載徹底!

[root@localhost ~]# which nginx

/usr/bin/which: no nginx in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)