四、Nginx的安裝

1、安裝步驟:html

1.下載系統對應的版本nginx

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

2.安裝環境的準備c++

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

3.檢查是否安裝了Nginxweb

find -name nginx

yum remove nginx

4.解壓shell

tar -zxvf nginx-1.12.0.tar.gz

5. 編譯和安裝vim

cd nginx-1.12.0
./configure
make
make install

6. 查看版本windows

/usr/local/nginx/sbin/nginx -V

7. 啓動重啓bash

/usr/local/nginx/sbin/nginx //啓動
/usr/local/nginx/sbin/nginx -s reload //重啓
netstat -nltp | grep 80 //查看監聽端口

8. 訪問Nginx服務器服務器

http://localhost

9. 中止tcp

/usr/local/nginx/sbin/nginx -s stop //快速中止
/usr/local/nginx/sbin/nginx -s quit //完整中止
ps -ef | grep nginx //查看全部進程的全面進程
kill -quit 主進程號 //中止進程
kill -term 主進程號 //快速中止
kill -9 nginx //強制中止

10. 添加防火牆例外

//將80端口添加爲防火牆例外
vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT //新增到文件中
/etc/init.d/iptables restart//重啓防火牆

2、Nginx配置

1.編寫shell腳本

# 添加修改此文件:vi /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
# 注意下面的兩行要修改爲你的實際路徑
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

2. 設置文件的訪問權限

chmod a+x /etc/init.d/nginx

3. 加入到rc.local文件中

# 修改vi /etc/rc.local,添加以下內容
/etc/init.d/nginx start

4. 設置開機啓動

chkconfig --add nginx #添加系統服務
chkconfig --level 345 nginx on #設置開機啓動,啓動級別
chkconfig --list nginx #查看開機啓動配置信息

3、 經過端口區分不一樣的虛擬主機

1. 修改nginx的配置文件,添加以下內容

# 在配置文件中添加server節點:寫入多份server節點,其端口號不一樣來區分虛擬主機
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  localhost;

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

2. 重啓服務

/usr/local /nginx/sbin/nginx -s reload

4、 經過域名區分不一樣的虛擬主機

1. 本地windows電腦,修改 C:\Windows\System32\drivers\etc\hosts文件

192.168.31.117       manager.dhc.com
192.168.31.117       portal.dhc.com

2. 服務器Centos7中,修改/usr/local/nginx/conf/nginx.conf文件(在對應路徑下添加mm和pp目錄便可)

server {
        listen       80;
        server_name  manager.dhc.com;

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

    server {
        listen       80;
        server_name  portal.dhc.com;

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

3. 重啓服務加載配置文件

/usr/local /nginx/sbin/nginx -s reload

 

1.
...
...
相關文章
相關標籤/搜索