linux安装部署nginx

该文详细介绍了在Linux环境下安装Nginx的过程,包括依赖包安装、下载、解压、编译、安装,然后修改配置文件,启动服务,以及如何创建服务脚本实现开机自启动。在过程中遇到了启动和停止服务的脚本错误,但最终成功解决并验证了Nginx的服务状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、环境准备


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

2、下载安装包

下载地址:

http://nginx.org/download/

3、解压安装

mkdir /usr/local/nginx

下载好的文件放到 /usr/local/nginx目录

cd /usr/local/nginx

tar -zxvf nginx-1.22.1.tar.gz

安装

#进入解压后的文件夹
cd /usr/local/nginx/nginx-1.22.1
#执行
./configure --with-http_stub_status_module --with-http_ssl_module
#安装
make

make install

4、启动

 ​​​​​​​/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

5、修改端口测试配置文件

端口改为8088:

vim /usr/local/nginx/conf/nginx.conf

重载配置文件:

/usr/local/nginx/sbin/nginx -s reload

 查看进程:

ps -ef | grep nginx

杀进程:

killall nginx

浏览器访问:http://ip:8088

6、制作成服务,设置开机自启

vim /etc/init.d/nginx

 内容:

#!/bin/bash
# chkconfig: 2345 85 15
# description: nginx Startup script for the Nginx HTTP Server
# processname: nginx
 
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
 

 添加可执行权限,

[root@fault122 init.d]# chmod +x /etc/init.d/nginx 

测试:

[root@fault122 init.d]# ./nginx start
./nginx: line 26: [: =: unary operator expected
Starting nginx:                                            [  OK  ]
[root@fault122 init.d]# ps -ef | grep nginx
root      19598      1  0 01:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    19600  19598  0 01:19 ?        00:00:00 nginx: worker process
root      19602  15970  0 01:19 pts/0    00:00:00 grep --color=auto nginx
[root@fault122 init.d]# ./nginx stop
./nginx: line 26: [: =: unary operator expected
Stopping nginx:                                            [  OK  ]
[root@fault122 init.d]# ps -ef | grep nginx
root      19631  15970  0 01:20 pts/0    00:00:00 grep --color=auto nginx
[root@fault122 init.d]# ./nginx restart
./nginx: line 26: [: =: unary operator expected
Stopping nginx:                                            [FAILED]
Starting nginx:                                            [  OK  ]
[root@fault122 init.d]# ./nginx status
./nginx: line 26: [: =: unary operator expected
nginx (pid 19651 19649) is running...

 设置开机自启动:

[root@fault122 init.d]# chkconfig nginx on
[root@fault122 init.d]# service nginx start
Starting nginx (via systemctl):                            [  OK  ]
[root@fault122 init.d]# service nginx stop
Stopping nginx (via systemctl):                            [  OK  ]
[root@fault122 init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nginx          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
redis          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

7、部署服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值