1. 源码安装nginx,并提供服务脚本
关闭防火墙及selinux
systemctl disable firewalld
setenforce 0
在http://nginx.org/download/ 下载nginx-1.18.0.tar.gz
解压安装
tar xf nginx-1.18.0.tar.gz -C /usr/local
解压到/usr/local
2.添加用户和组
useradd nginx -s /sbin/nologin -M
切到/usr/local/nginx/nginx-1.20.1
cd /usr/local/nginx/nginx-1.20.1/
3.源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)
./configure --user=nginx
./configure --group=nginx
./configure --prefix=/usr/local/nginx
./configure --with-http_stub_status_module
./configure --with-http_ssl_module
4.编译+安装
make && make install
5.为 nginx 提供 SysV init 脚本
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2.启动Nginx
创建好之后,我们就可以cd ngiux 命令进入到Nginx目录,可以看到三个文件夹第一个conf,配置文件所放的目录,html是我们静态资源放的目录,当然,这是默认的,我们也可以自己改,sbin目录就是存放了我们的启动文件👇
那么我们进入sbin这个文件,可以发现里边只有一个文件👇:
接着我们就可以使用 ./nginx 命令来启动Nginx了👇:
然后直打开浏览器访问,输入IP地址,然后因为他的端口号默认是80,所以敲 IP地址就可以了👇:
(如果访问不了,可能是因为被防火墙拦截了,需要使用firewall-cmd --zone=public --add-port=80/tcp --permanent 把80这个端口放行了,然后然后再使用firewall-cmd --reload来刷新一下重新访问即可)