linux 安装并配置nginx

 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

进入网页

linux 安装并配置nginx_第1张图片

 2. 配置基于域名的虚拟主机。

启动nginx

systemctl enable nginx

可使用systemctl  status nginx查看是否有问题

一般情况像这样就能在网页启动 

linux 安装并配置nginx_第2张图片

而且可以restart

配置文件(/usr/local/nginx/conf/nginx.conf)

linux 安装并配置nginx_第3张图片注意位置,错位会报错

 配置好之后,创建站点目录、主页文件

[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)

linux 安装并配置nginx_第4张图片

systemctl restart nginx

linux 安装并配置nginx_第5张图片

 基于用户

更改配置文件(/usr/local/nginx/conf/nginx.conf)

linux 安装并配置nginx_第6张图片

 给密码,重启nginx

yum install httpd-tools
htpasswd -c /usr/share/nginx/html/bbs/.pass biubiu
systemctl restart nginx

 linux 安装并配置nginx_第7张图片

 

4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。

 修改配置文件(/usr/local/nginx/conf/nginx.conf)

linux 安装并配置nginx_第8张图片

cd /usr/local/nginx/html
vim err.html

重启nginx

测试

你可能感兴趣的:(服务器,运维)