CentOS7.0 安裝 Nginx1.10.1 tar.gz方式

一.安裝準備

首先因爲nginx的一些模塊依賴一些lib庫,因此在安裝nginx以前,必須先安裝這些lib庫,這些依賴庫主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 因此執行以下命令安裝html

yum install gcc-c++  
yum install pcre pcre-devel  
yum install zlib zlib-devel  
yum install openssl openssl--devel

缺乏openssl的話,則會影響後面在nginx服務器上部署ssl證書nginx

二.安裝nginx

1.在安裝以前,先查看下是否已經安裝了nginxc++

find -name nginx

2.若是有nginx,先卸載apache

yum remove nginx

若是出現沒法卸載的狀況,直接把nginx所在的文件夾刪除(待驗證)瀏覽器

3.進入 /usr/local 目錄    cd /usr/local服務器

4.利用wget從官網下載nginx-1.10.1tcp

wget http://nginx.org/download/nginx-1.10.1.tar.gz

 下載完畢後利用 tar 命令解壓測試

tar -zxvf nginx-1.10.1.tar.gz

解壓完成後 cd到解壓目錄spa

cd nginx-1.10.1

5.安裝,使用 --prefix 參數指定nginx安裝目錄(默認在 /usr/local/nginx 下), make,make install 執行安裝code

./configure
make
make install

6.安裝完畢後,查看nginx在哪

whereis nginx

三.配置,啓動nginx

1.進入nginx安裝目錄

cd /usr/local/nginx/

2.啓動nginx:  直接執行 ./nginx 打開瀏覽器輸入 localhost 查看

在未修改 nginx.conf配置文件的時候,會出現上圖的默認頁面

若是出現沒法訪問狀況,檢查下 80端口是否開放,若未發放,則需添加規則.

CentOS7 默認使用 firewalld 防火牆,若想使用iptables,則需關閉Firewalld,並安裝 iptables,具體過程這裏很少說.在iptables下,添加80端口,並保存

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save

而後從新訪問 localhost 

3.修改nginx配置文件 nginx.conf 這裏僅修改 / 用於測試

打開 nginx.conf文件,修改 server中的   location /  中的配置

server {
        listen       80;   #監聽端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
	
	location /login {
		rewrite (.*) https://www.mmtvip.com$1 permanent;
        }
	
	location /front/invest {
		rewrite (.*) https://www.mmtvip.com$1 permanent;
        }
}

保存並覆蓋後,使用命令 ./nginx -s reload 來更新配置文件

./nginx -s reload

3.啓動nginx  直接執行 ./nginx 打開瀏覽器輸入 localhost 查看
 

在3.2 中修改了 location / 的規則後,在瀏覽器中輸入  localhost/login

則瀏覽器會跳轉到 https://www.mmtvip.com/login界面,同時瀏覽器地址也相應變換

同理,輸入第二個 localhost/front/invest ,瀏覽器會自動跳轉到 https://www.mmtvip.com/front/invest界面

四.中止,重啓nginx

1.查看nginx進程 pid

ps -ef | grep nginx

2.中止nginx,其實就是kill 掉nginx的進程,可是不能用kill -9 pid 這樣,由於nginx啓動時有子進程

圖中nginx的master進程id爲 6459,因此 執行 kill 6459

3.啓動nginx

cd /usr/local/nginx/sbin
./nginx
相關文章
相關標籤/搜索