1、环境准备
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2、下载安装包
下载地址:
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、部署服务