一、安装
1、手动建下载好的安装包放到linux服务器上
下载地址:http://nginx.org/download/;源码安装选择.tar.gz结尾的安装包,如http://nginx.org/download/nginx-1.17.3.tar.gz
2、解压安装包
tar -zvxf nginx-1.17.3.tar.gz
3、创建安装文件夹
mkdir /home/nginx
4、安装编译环境
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
5、源码安装软件时配置环境用,根据你的配置选项和你的系统情况生成makefile文件,为make做准备
./configure --prefix=/home/nginx
6、编译
make
7、安装
make install
二、测试安装
1、跳转到nginx的安装位置
cd /home/nginx/sbin
2、查看nginx是否启动(未启动)
[root@localhost sbin]# ps -ef|grep nginx
root 4100 1368 0 23:46 pts/0 00:00:00 grep --color=auto nginx
3、启动nginx
./nginx 或者 ./nginx -c /home/nginx/conf/nginx.conf
4、查看nginx是否启动(已启动,含主进程和后台进程)
[root@localhost sbin]# ps -ef|grep nginx
root 4102 1 0 23:48 ? 00:00:00 nginx: master process ./nginx
nobody 4103 4102 0 23:48 ? 00:00:00 nginx: worker process
root 4105 1368 0 23:49 pts/0 00:00:00 grep --color=auto nginx
5、访问nginx方式查看启动
curl http://127.0.0.1/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
三、Nginx设置成服务并开机自动启动
1、添加服务到service
vim /lib/systemd/system/nginx.service
2、编写nginx.service中的内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/home/nginx/sbin/nginx
ExecReload=/home/nginx/sbin/nginx -s reload
ExecStop=/home/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3、添加启动文件
vim /etc/init.d/nginx
4、填写内容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.1.17.3 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=/home/nginx/sbin/nginx
nginx_config=/home/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
5、保存后设置文件的执行权限
chmod 777 /etc/init.d/nginx
6、启动或停止nginx(可能无法停止,因为守护进程存在)
/etc/init.d/nginx start
/etc/init.d/nginx stop
7、步骤4中的方式虽能启动,但是也不太方便,添加到服务中开机自启动。
chkconfig --add /etc/init.d/nginx
8、设置开机自启动
chkconfig nginx on
9、查看自启动服务列表
chkconfig --list
10、通过服务方式管理nginx,相关命令:
service nginx start
service nginx stop
service nginx restart
11、同时也可使用systemctl(管理systemd的资源Unit,即.service文件,兼容service命令)进行管理,常见命令:
# systemctl start nginx 启动服务
# systemctl stop nginx 停止服务
# systemctl restart nginx 重启服务
# systemctl status nginx 查看服务当前状态
# systemctl list-units --type=service 查看所有已启动的服务