centos7安装nginx并设置开机自启

1.先安装好nginx所需要的安装环境

gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境

yum install gcc-c++ 

PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http)

yum install -y openssl openssl-devel

zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip

yum install -y zlib zlib-devel

2.编译安装

上传nginx-1.8.0.tar.gz到/usr/local中
在这里插入图片描述
解压

tar -zxvf nginx-1.8.0.tar.gz

先在/var下创建temp及nginx目录,即/var/temp/nginx

cd nginx-1.8.0
进行参数设置:
./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

此时makefile存在
在这里插入图片描述
编译安装

make
make  install   根据上面的参数,安装nginx到/usr/local/nginx

查看目录
在这里插入图片描述

3.启动nginx

cd  /usr/local/nginx/sbin
./nginx

启动成功(设置的端口是默认的80端口,所以直接输入服务器ip即可访问)
注意:防火墙要开放80端口
在这里插入图片描述停止nginx

cd /usr/local/nginx/sbin
./nginx -s stop

重启nginx

./nginx -s reload

4.配置nginx开机启动

 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: /usr/local/nginx/logs/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=/usr/local/nginx/logs/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 /usr/local/nginx/logs/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

设置文件访问权限

chmod a+x /etc/init.d/nginx

操作nginx
在这里插入图片描述

vi /etc/rc.local

加入一行  /etc/init.d/nginx start    保存并退出

对rc.local文件增加可执行权限
在这里插入图片描述重启后查看

ps aux | grep nginx
或者  /etc/init.d/nginx status

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43969229/article/details/88529586
今日推荐