1. 源码安装nginx,并提供服务脚本。
关闭防火墙及selinux
systemctl disable firewalld
setenforce 0
在http://nginx.org/download/
下载nginx-1.20.1.tar.gz
tar xf nginx-1.20.1.tar.gz -C /usr/local
解压到/usr/local
添加用户和组
useradd nginx -s /sbin/nologin -M
切到/usr/local/nginx/nginx-1.20.1
cd /usr/local/nginx/nginx-1.20.1/
源码的安装一般由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
编译+安装make && make install
为 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
更改端口号
vim /usr/local/nginx/conf/nginx.conf
重启虚拟机reboot
启动nginx
systemctl enable nginx
进入网页
2. 配置基于域名的虚拟主机。
启动nginx
systemctl enable nginx
可使用systemctl status nginx查看是否有问题
一般情况像这样就能在网页启动
而且可以restart
配置文件(/usr/local/nginx/conf/nginx.conf)
注意位置,错位会报错
配置好之后,创建站点目录、主页文件
[root@localhost nginx]# for name in blog bbs;do mkdir html/$name;done
[root@localhost nginx]# for name in blog bbs ;do echo " $name test" > html/$name/index.html ;done
重启服务
systemctl restart nginx
配置hosts文件(/etc/hosts)
测试
3. 配置nginx基于用户和地址的访问控制。
基于地址
更改配置文件(/usr/local/nginx/conf/nginx.conf)
systemctl restart nginx
基于用户
更改配置文件(/usr/local/nginx/conf/nginx.conf)
给密码,重启nginx
yum install httpd-tools
htpasswd -c /usr/share/nginx/html/bbs/.pass biubiu
systemctl restart nginx
4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。
修改配置文件(/usr/local/nginx/conf/nginx.conf)
cd /usr/local/nginx/html
vim err.html
重启nginx
测试