Nginx安装

一、Nginx安装

1、依赖包

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget vim telnet net-tools

2、下载1.22.0版本安装包

cd /opt/soft && wget http://nginx.org/download/nginx-1.22.0.tar.gz

3、解压

tar -zxvf nginx-1.22.0.tar.gz -C /opt/module

4、创建Nginx用户

useradd -s /sbin/nologin -M nginx

5、编译

cd /opt/module/nginx-1.22.0

6、安装

make && 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 \
--with-http_sub_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

7、启动Nginx

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

8、停止Nginx

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

9、重新加载Nginx

cd /usr/local/nginx/sbin/ && ./nginx -s reload

10、访问Web

http://192.168.66.101
curl http://192.168.66.101

二、Systemctl管理Nginx

1、nginx脚本

vim /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

2、赋予权限

chmod 777 /etc/init.d/nginx

3、添加服务

chkconfig --add nginx

4、启动/停止/重启Nginx

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

三、题外话(Nginx升级,暂时无需操作)

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

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

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

tar -zxvf nginx-1.22.2.tar.gz -C /opt/module

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

cd /opt/module/nginx-1.22.2
./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 \
--with-http_sub_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

4、执行make

make

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

cp -y /opt/module/nginx-1.22.2/nginx-1.22.2/objs/nginx /usr/local/nginx/sbin/

6、测试是否替换成功

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -V

参考地址:https://blog.csdn.net/wangzongyu/article/details/127076960

posted @ 2023-04-10 14:38  宜家数据小哥  阅读(112)  评论(0编辑  收藏  举报