关于Nginx的安装和环境部署

阅读目录:

1. 简介

2. 关闭防火墙和Selinux

3. 下载并安装Nginx 1.12.x版本

4. 启动并验证

5.Nginx常用命令

6.配置Nginx 1.12.x自启动

7.说明

1. 简介

    作为一款优秀的web服务器

  1. 可支持Rails和php的程序
  2. 可作为http反向代理服务器
  3. 作为负载均衡服务器
  4. 作为邮件代理服务器
  5. 帮助实现前端动静分离

2. 关闭防火墙和Selinux

        Linux的防火墙是咱们新手的噩梦,很多情况会出现能ping通,但是访问不了Web页面。所以开始就干掉它!

    2.1 关闭防火墙

    [root@localhost ~]# /etc/init.d/iptables stop
    iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
    iptables: Flushing firewall rules:                         [  OK  ]
    iptables: Unloading modules:                               [  OK  ]

    2.2 开机自动关闭防火墙

    [root@localhost ~]# chkconfig iptables off

    2.3 查看Selinux状态

    [root@localhost ~]# sestatus
    SELinux status: enabled 
    SELinuxfs mount: /sys/fs/selinux 
    SELinux root directory: /etc/selinux 
    Loaded policy name: targeted 
    Current mode: enforcing 
    Mode from config file: enforcing 
    Policy MLS status: enabled 
    Policy deny_unknown status: allowed 
    Max kernel policy version: 28

   2.4 关闭selinux

    [root@localhost ~]# vi /etc/selinux/config 

修改 SELINUX=disabled ,重启机器。
注:永久开启->改成:SELINUX=enforcing

3. 下载并安装Nginx 1.12.x版本

注:为了方便管理,创建一个文件夹专门放所需软件

    [root@localhost /]# mkdir developer
    [root@localhost /]# cd developer

    3.1 安装前的环境准备

    [root@localhost developer]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel gcc gcc-c++ autoconf automake make

    3.2 下载Nginx 1.12.x

    [root@localhost developer]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

注:Nginx 1.12.x 下载官网url如下:

    http://nginx.org/en/download.html

    3.3 解压Nginx 1.12.x

    [root@localhost developer]# tar -zxvf nginx-1.12.2.tar.gz

    3.4 进入Nginx目录

    [root@localhost developer]# cd nginx-1.12.2

   3.5 对安装的软件进行配置,并检验安装依赖

    [root@localhost nginx-1.12.2]# ./configure

    3.6 编译

    [root@localhost nginx-1.12.2]# make

   3.7 安装

    [root@localhost nginx-1.12.2]# make install

4. 启动并验证

    4.1 编辑nginx配置文件

    [root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf

在# another virtual host using mix of IP-, name-, and port-based configuration 上面

增加include vhost/*.conf;

    4.2 增加配置文件

    [root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf
    [root@localhost conf]# mkdir vhost

    4.3 启动nginx

    [root@localhost conf]# cd /usr/local/nginx/sbin
    [root@localhost sbin]# ./nginx

    4.4 验证Nginx是否启动

    方式1:打开浏览器,输入ip

                比如:192.168.163.129
    方式2:检查端口号,或者进程

    检查端口号,有显示就表示成功

    [root@localhost nginx-1.12.2]# netstat -an | grep 80

    检查进程

    [root@localhost nginx-1.12.2]# ps -ef | grep nginx

5. Nginx常用命令

    5.1 测试配置文件

    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t

    5.2 启动命令

    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx

   5.3 停止命令

    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s stop

或者:

    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s quit

   5.4 重启命令

    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s reload

   5.5 查看进程命令

    [root@localhost nginx-1.12.2]# ps -ef|grep nginx

   5.6 平滑重启

    [root@localhost nginx-1.12.2]# /kill -HUP +查看进程命令查到的PID

   5.7 测试Nginx配置文件是否有问题

    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t

6. 配置Nginx 1.12.x自启动

   6.1 创建编辑nginx启动文件

    [root@localhost nginx-1.12.2]# vim /etc/init.d/nginx

    6.2 复制脚本

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemin

#

# chkconfig: - 85 15

# description: Nginx is an HTTP(S) server, HTTP(S) reverse

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: /usr/local/nginx/conf/nginx.conf

# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

#nginx所在路径

nginx="/usr/local/nginx/sbin/nginx"

#nginx配置文件路径

prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

echo -n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq 0 ] && touch $lockfile

return $retval

}

stop() {

echo -n $"Stopping $prog: "

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile

return $retval

}

restart() {

configtest || return $?

stop

start

}

reload() {

configtest || return $?

echo -n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null 2>&1

}

case "$1" in

start)

rh_status_q && exit 0

$1

;;

stop)

rh_status_q || exit 0

$1

restart|configtest)

$1
retval=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile

return $retval

}

restart() {

configtest || return $?

stop

start

}

reload() {

configtest || return $?

echo -n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null 2>&1

}

case "$1" in

start)

rh_status_q && exit 0

$1

;;

stop)

rh_status_q || exit 0

$1

restart|configtest)

$1

;;

reload)

rh_status_q || exit 7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit 0

;;

*)

echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

exit 2

esac
 

  6.3 赋予权限

    [root@localhost nginx-1.12.2]# chmod a+x /etc/init.d/nginx

  6.4 将nginx服务加入chkconfig管理列表

    [root@localhost nginx-1.12.2]# chkconfig --add /etc/init.d/nginx

7. 说明

    说明:本次使用

       操作系统:CentOS 6.8 64位

       Nginx版本:1.12.2

posted @ 2020-06-06 21:47  神奇海螺c  阅读(230)  评论(0编辑  收藏  举报
/* 看板娘 */
你要干嘛呀?