5、阿里云Esc服务器安装Nginx-1.13.7

一、Nginx的安装

1.下载  : wget  http://nginx.org/download/nginx-1.13.7.tar.gz

2.安装环境配置

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

3.解压  : tar -zxvf nginx-1.13.7.tar.gz

4.编译和安装:

cd nginx-1.13.7  (即解压后的nginx文件中)

./configure     编译

先 make 再 make install

5.  查看版本  :/usr/local/nginx/sbin/nginx -V

6. 启动与重启 :

/usr/local/nginx/sbin/nginx       启动

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

7. 访问Nginx服务器

http://localhost(即阿里云的公有IP)

 

成功如下图:

 

135841_rFWP_3678587.png

8. 查看进程与停止:

/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                                                    强制停止

9. 添加防火墙例外

// 将80端口为防火墙例外

vim  /etc/sysconfig/iptables

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCERT                 新增到文件中

/etc/init.d/iptables restart                                                                                     重启防火墙

 

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

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

1.修改nginx的配置文件(即 /usr/local/nginx/conf/nginx.conf ),添加以下内容

# 在配置文件中添加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

 

  •   通过域名区分不同的虚拟主机
  • 注意,域名解析规则:
一个域名对应一个IP地址,一个IP地址可以被多个域名绑定

 

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

 

 

转载于:https://my.oschina.net/u/3678587/blog/1609546

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值