您的位置:首页 > 运维架构 > Nginx

linux下安装nginx服务器

2017-12-31 14:01 399 查看
1.安装环境

本次使用rpm,也可以使用源码进行安装

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

yum install gcc-c++

2.下载nginx(/usr/local目录下)

wget https://nginx.org/download/nginx-1.11.11.tar.gz     tar完整的,否则安装失败

3.解压(/usr/local目录下)

tar -zxvf  nginx-1.11.11.tar.gz

4.重命名(/usr/local目录下)

rename nginx-1.11.11 nginx nginx-1.11.11

5.配置(/usr/local/nginx目录下)

./configure

6.安装(/usr/local/nginx目录下)

make


7.创建日志文件logs(/usr/local/nginx目录下)

mkdir logs

8.查看nginx是否安装成功

 cd  /usr/local/nginx/sbin

./nginx -t 

如果显示如下内容代表安装成功

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

9.启动nginx(未配置系统启动nginx下启动方法)

 cd  /usr/local/nginx/sbin

./nginx   启动nginx 

./nginx -s stop  关闭nginx

./nginx -s quit  退出

./nginx -s reload  重启reload

10.给防火墙添加端口

firewall-cmd --zone=public --add-port=80/tcp --permanen   添加端口

firewall-cmd --reload   重启防火墙

80端口无需在阿里服务器进行配置端口,默认已开通

浏览器中输入ip地址就可以显示出如下内容:说明已经安装好了



11.添加系统环境变量

vim /etc/profile

加入如下内容:

PATH=$PATH:/usr/local/nginx/sbin

export PATH

保存关闭后运行 source /etc/profile 环境变量并生效

12.新增系统服务

cd   /etc/rc.d/init.d

touch nginx

vi nginx

#!/bin/bash  

# chkconfig: 2345 10 90  

# description: nginx  

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

function show_usage(){  

        usage="Usage: `basename $0` [ start|stop|restart|reload ]"  

        echo $usage  

        exit  

}  

base_dir=/usr/local/nginx/  

[[ $# != 1 ]] && show_usage  

  

nginx=${base_dir}sbin/nginx  

case $1 in  

start)  

        [[ `netstat -ntlup|grep nginx|wc -l` > 0 ]] && failure && echo "Nginx is Running!" && exit  

        echo "Starting Nginx..."  

        $nginx  

        ([ $? -eq 0 ] && success && echo "Nginx starts successfully!") || (failure && echo "Failed start nginx")  

;;  

stop)  

        [[ `netstat -ntlup|grep nginx|wc -l` == 0 ]] && failure && echo "Nginx is NOT running!" && exit  

        echo "Stopping Nginx..."  

        $nginx -s stop  

        ([ $? -eq 0 ] && success && echo "Nginx stops successfully!") || (failure && echo "Failed stop nginx!")  

  

;;  

restart)  

        if [[ `netstat -ntlup|grep nginx|wc -l` == 0 ]]; then  

                echo "Starting Nginx..."  

                $nginx  

                [ $? -eq 0 ] && success && echo "Nginx starts successfully!"  

        else  

                echo "Stopping Nginx..."  

                $nginx -s stop  

                ([ $? -eq 0 ] && success && echo "Nginx stops successfully!") || (failure && echo "Failed stop nginx!")  

                echo "Starting Nginx..."  

                $nginx  

                ([ $? -eq 0 ] && success && echo "Nginx starts successfully!") || (failure && echo "Failed start nginx")  

        fi  

;;  

reload)  

        [[ `netstat -ntlup|grep nginx|wc -l` == 0 ]] && failure && echo "Nginx is NOT Running!" && exit  

        $nginx -s reload  

        [ $? -eq 0 ] && success && echo "Nginx reloads successfully!"  

;;  

*)  

        show_usage  

;;  

esac  

chkconfig --add nginx

chkconfig  nginx on

13.系统服务

service nginx start   启动

service nginx stop  关闭

service nginx restart  重启
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: