4、Nginx的安装

一、安装步骤:

1.下载系统对应的版本

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

2.安装环境的准备

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

3.检查是否安装了Nginx

find -name nginx

yum remove nginx

4.解压

tar -zxvf nginx-1.12.0.tar.gz

5. 编译和安装

cd nginx-1.12.0
./configure
make
make install

6. 查看版本

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

7. 启动重启

/usr/local/nginx/sbin/nginx //启动
/usr/local/nginx/sbin/nginx -s reload //重启
netstat -nltp | grep 80 //查看监听端口

8. 访问Nginx服务器

http://localhost

9. 停止

/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. 添加防火墙例外

扫描二维码关注公众号,回复: 411975 查看本文章
//将80端口添加为防火墙例外
vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT //新增到文件中
/etc/init.d/iptables restart//重启防火墙

二、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 #查看开机启动配置信息

三、 通过端口区分不同的虚拟主机

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

四、 通过域名区分不同的虚拟主机

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

猜你喜欢

转载自my.oschina.net/u/3678587/blog/1625310