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

进入网页

 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

测试

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值