在centos7环境搭建nginx服务

1、环境准备

  需要安装gcc环境、pcre库、zlib库、openssl密码库

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

2、安装nginx

  (1)下载nginx压缩包

   登录http://nginx.org/en/download.html,如下图所示,点击对应版本下载,选择中间进行下载

下载完成后是一个tar.gz的压缩包。

(2)上传至Linux环境

利用xftp工具上传文件至Linux环境的/usr/local/src/下

(3)进入/usr/local/src/目录下,进行解压,解压命令:

tar -zxvf nginx-1.17.2.tar.gz

(4)配置nginx

进入nginx解压目录下,执行以下代码:

./configure --prefix=/usr/local/nginx

(5)编译

命令:make 或者make install

(6)查看安装目录

3、启动nginx

(1)启动命令 

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

(2)查看启动状态 命令:ps -ef | grep nginx

这里有两个进程,其中master表示主进程,worker表示工作进程。

(3)停止nginx服务  命令

./nginx -s quit

(4)重启nginx服务  命令

./nginx -s quit
./nginx

(5)网页测试,打开浏览器,输入ip

如图所示代表安装成功。

4、配置开机自启动

(1)命令: 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)nginx服务相关命令

//启动命令
systemctl start nginx

//停止命令
systemctl stop nginx   

//查看运行状态
systemctl status nginx 

(3)开机启动,加入rc.local文件中

      vi /etc/rc.local

  加入一行  /etc/init.d/nginx start    

  保存并退出,下次重启系统会生效。

5、开启http服务,自定义启动路径

命令:vi /usr/local/nginx/conf/nginx.conf

找到location下的root目录:

修改之前:

修改后,添加自定义目录(随意,我这里是: /usr/local/src)

 

然后保存此文件。

登录浏览器测试,可以向自定义文件下添加一图片,我这里添加了一张命名为 1.jpg的图片

需要重启nginx,使配置文件生效。然后登录方式嘛,自然发生变化,我这里的登录网址是:http://192.168.79.167/1.jpg

就登录成功了,如果这样自定义配置了,就不能直接利用ip进行登录了,如若需要ip登录,就把/usr/local/nginx/conf/nginx.conf这个配置文件修改回去即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值