Nginx服务安装与启动脚本配置

本文详细介绍了在RHEL7.2环境下从零开始安装配置Nginx服务器的过程,包括创建用户、安装依赖包、编译安装Nginx、配置启动脚本及设置开机自启等关键步骤。

实验环境:RHEL7.2 x64-176,IP地址:192.168.1.176
实验工具:wKiom1eIIiyy4xGGAAANt1ehlE0333.jpg-wh_50

实验步骤:

1、安装nginx服务器
2、配置nginx启动脚本
3、文件设置并验证结果

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1、安装nginx服务器

[root@localhost~]# useradd nginx -s /sbin/nologin -M   #创建 nginx用户

[root@localhost~]# id nginx       #验证

uid=1001(nginx)gid=1001(nginx) 组=1001(nginx)
[root@localhost ~]# yum install pcre pcre-devel openssl openssl-devel -y   #安装依赖相关包
[root@localhost ~]# tar xf nginx-1.9.14.tar.gz  #解压nginx源码包到当前目录
[root@localhost ~]# cd nginx-1.9.14/    #进入解压后的nginx目录
[root@localhost nginx-1.9.14]# ./configure --user=nginx --group=nginx  #配置nginx

[root@localhostnginx-1.9.14]#make && make install #编译安装nginx

[root@localhostnginx-1.9.14]# /usr/local/nginx/sbin/nginx -t  #检查语法

[root@localhostnginx-1.9.14]# /usr/local/nginx/sbin/nginx #启动nginx

#访问虚拟机IP地址测试出现 nignx 成功
wKiom1eIImXSOFkUAABoxFaO-3k129.png-wh_50

2、配置nginx启动脚本
[root@localhost~]# vim nginx

==============================================================
#! /bin/bash

# chkconfig:2345 99 20

#description:nginx-server

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

case $1 in

 

    start)

        netstat -anlpt | grep nginx

        if [ $? -eq 0 ]

            then

                echo "nginx-server isalready running"

            else

                echo "nginx-server beginstart"

                   $nginx

         fi

    ;;

    stop)

        $nginx -s stop

        if [ $? -eq 0 ]

            then

                echo "nginx-server isstoped"

            else

                echo "nginx-server stopfail,try again"

        fi

        ;;

    status)

        netstat -anlpt | grep nginx

        if [ $? -eq 0 ]

            then

                echo "nginx-server isrunning"

            else

                echo "nginx-server isstoped"

            fi

        ;;

    restart)

        $nginx -s reload &>/dev/null

        if [ $? -eq 0 ]

            then

                echo "nginx-server isbegin restart"

            else

                echo "nginx-server restartfail"

        fi

        ;;

    *)

                echo "please enter {startrestart status stop}"

        ;;

esac

exit 0 
================================================================

3、文件设置并验证结果

[root@localhost~]# cp nginx /etc/init.d/  #将启动文件复制到/etc/init.d目录下
[root@localhost ~]# chmod +x /etc/init.d/nginx  #设置可执行权限
[root@localhost ~]# chkconfig --add nginx #将nginx添加为系统的服务
[root@localhost ~]# chkconfig --list nginx #查看nginx的开机运行级别

wKioL1eIIlOgJN0QAABJbr3l2so047.png-wh_50

本文转自xinsz08の平行时空博客51CTO博客,原文链接http://blog.51cto.com/xinsz08/1826562如需转载请自行联系原作者


维度2018

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值