在CentOS7.9安装与配置Nginx1.22.0

1. Nginx的安装

  Nginx官网:http://nginx.org/en/download.html
  ● Mainline version:正处于开发状态版本
  ● Stable version :稳定版本
  ● Legacy version :老版本

  安装依赖软件包:

yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 安装pcre库用于解析正则表达式
# 安装zlib包用于压缩和解压缩
# 安装openssl是安全的加密的套接字协议层(SSL),用于HTTP安全传输,也就是https

  下载软件包并安装:

  创建nginx用户:

 useradd -s /sbin/nologin -M nginx
wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz
cd nginx-1.22.0

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--pid-path=/var/run/nginx/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 \
--with-http_gunzip_module  \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--http-client-body-temp-path=/usr/local/nginx/client \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi
make && make install 

2. 启动并配置Nginx服务

2.1. 脚本方式启动Nginx

cd /usr/local/nginx/sbin/
启动nginx:	   ./nginx 
停止nginx:	   ./nginx -s stop
重新加载nginx: ./nginx -s reload
/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

  打开浏览器,访问服务器的IP地址即可打开nginx的默认页面。nginx的根路径在:/usr/local/nginx/html/
在这里插入图片描述

2.1. 把nginx配置成一个服务

cat /etc/init.d/nginx
#!/bin/sh
#chkconfig: - 85 15

PATH=/usr/local/nginx/sbin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}

do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}

do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

  添加权限:

chmod 777 /etc/init.d/nginx

  添加服务:

chkconfig --add nginx

  启动nginx服务:

systemctl start nginx.service
systemctl stop nginx.service
systemctl restart nginx.service

systemctl status nginx
● nginx.service - (null)
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (running) since Tue 2022-09-27 20:02:37 CST; 1s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 4204 ExecStop=/etc/rc.d/init.d/nginx stop (code=exited, status=0/SUCCESS)
  Process: 4206 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─4208 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─4209 nginx: worker process

3. Nginx升级

  备份原nginx二进制文件,切换到nginx所在目录进行备份,备份的过程系统不受影响

 cp  -pdr /usr/local/nginx  /usr/local/nginx.bak

  将新版本的nginx压缩包上传到服务器进行解压:

 tar xvf nginx-1.22.2.tar.gz

  切换到解压后的nginx文件夹内执行,只需要make,千万不要make install ,如果执行make install会将原来的配置文件覆盖

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--pid-path=/var/run/nginx/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 \
--with-http_gunzip_module  \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--http-client-body-temp-path=/usr/local/nginx/client \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi

make 

  复制nginx-1.22.2下的nginx到原来的nginx目录下,注意nginx-1.22.2中nginx在objs目录下而不是在sbin下面

cp /root/nginx-1.22.2/objs/nginx /usr/local/nginx/sbin/
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y

  测试是否替换成功:

/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

/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.22.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --without-http_rewrite_module --without-http_gzip_module
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

降世神童

学都学了,看也看了,感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值