经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
CentOS7---Nginx安装并配置虚拟主机
来源:cnblogs  作者:ChAnAn  时间:2023/4/17 9:17:19  对本文有异议

1、源码安装nginx,并提供服务脚本

源码包的获取:http://nginx.org/download/

实验环境:和企业环境类似,关闭防火墙,禁用selinux,使用静态IP地址

环境准备:

步骤一:关闭防火墙和selinux

  1. # 设置为开机不启动
  2. [root@node01 ~]# systemctl disable firewalld.service
  3. # 临时停止防火墙
  4. [root@node01 ~]# systemctl stop firewalld.service
  5. # 手动停止selinux,可以不用重启动虚拟机
  6. [root@node01 ~]# setenforce 0
  7. [root@node01 ~]# vim /etc/selinux/config
  8. [root@node01 ~]# sestatus
  9. SELinux status: disabled
  10. # 检查状态
  11. [root@node01 ~]# systemctl is-active firewalld.service
  12. unknown
  13. [root@node01 ~]# getenforce
  14. Disabled

步骤二:配置静态IP地址

  1. # 修改为静态地址,注意子网掩码
  2. [root@node01 ~]# nmcli connection modify ens32 ipv4.method manual ipv4.addresses 192.168.11.110 ipv4.gateway 192.168.11.2 ipv4.dns 8.8.8.8 connection.autoconnect yes
  3. # 启动网卡
  4. [root@node01 ~]# nmcli connection up ens32

步骤三:安装常用软件

  1. [root@node01 ~]# yum install -y bash-completion tree lrzsz vim net-tools.x86_64 unzip net-tools lsof wget

安装Nginx步骤:

步骤一:安装Nginx所需的pcre库

  1. [root@node01 ~]# yum install pcre-devel -y

步骤二:安装依赖包

  1. [root@node01 ~]# yum -y install gc gcc gcc-c++ zlib-devel openssl-devel

步骤三:创建用户和用户组

  1. [root@node01 ~]# groupadd nginx
  2. [root@node01 ~]# useradd -s /sbin/nologin -g nginx -M nginx

步骤四:上传文件并解压到指定目录

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了
很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的
检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台

  1. [root@node01 ~]# wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
  2. [root@node01 ~]# tar xf tengine-2.2.0.tar.gz -C /usr/local/src/
  3. [root@node01 ~]# cd /usr/local/src/tengine-2.2.0/
  4. [root@node01 tengine-2.2.0]#
  5. [root@node01 tengine-2.2.0]# ls
  6. AUTHORS.te CHANGES.cn conf docs man README tests
  7. auto CHANGES.ru configure html modules README.markdown THANKS.te
  8. CHANGES CHANGES.te contrib LICENSE packages src

步骤五:编译安装

  1. ./configure --user=nginx --group=nginx --prefix=/usr/local/src/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module

步骤六:make && make install

  1. [root@node01 tengine-2.2.0]# make && make install

步骤七:修改目录权限

  1. [root@node01 tengine-2.2.0]# chown -R nginx.nginx /src/tengine-2.2.0/

服务脚本:

  1. [root@node01 ~]# cat /usr/lib/systemd/system/nginx.service
  2. t]
  3. Description=nginx - high performance web server
  4. Documentation=http://nginx.org/en/docs/
  5. After=network.target remote-fs.target nss-lookup.target
  6. [Service]
  7. Type=forking
  8. PIDFile=/usr/local/src/nginx/logs/nginx.pid
  9. ExecStartPre=/usr/local/src/nginx/sbin/nginx -t -c /usr/local/src/nginx/conf/nginx.conf
  10. ExecStart=/usr/local/src/nginx/sbin/nginx -c /usr/local/src/nginx/conf/nginx.conf
  11. ExecReload=/bin/kill -s HUP $MAINPID
  12. ExecStop=/bin/kill -s QUIT $MAINPID
  13. PrivateTmp=true
  14. [Install]
  15. WantedBy=multi-user.target

修改了PID文件

  1. # 重新创建了一个PID文件
  2. touch /usr/local/src/nginx/logs/nginx.pid

脚本测试:

  1. [root@node01 ~]# systemctl daemon-reload
  2. [root@node01 ~]# systemctl restart nginx.service
  3. [root@node01 ~]#
  4. [root@node01 ~]#
  5. [root@node01 ~]# ss -lntup | grep 80
  6. tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=13454,fd=6),("nginx",pid=13452,fd=6))
  7. tcp LISTEN 0 80 [::]:3306 [::]:* users:(("mysqld",pid=1202,fd=28))
  8. [root@node01 ~]#
  9. [root@node01 ~]#
  10. [root@node01 ~]# systemctl stop nginx.service
  11. [root@node01 ~]#
  12. [root@node01 ~]#
  13. [root@node01 ~]# ss -lntup | grep 80
  14. tcp LISTEN 0 80 [::]:3306 [::]:* users:(("mysqld",pid=1202,fd=28))

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

步骤一:进入默认主页路径

Nginx的默认路径和Apache的不一样/usr/local/src/nginx/html/

  1. [root@node01 ~]# cd /usr/local/src/nginx/html/
  2. [root@node01 html]# ll
  3. total 8
  4. -rw-r--r-- 1 root root 539 Apr 16 18:07 50x.html
  5. -rw-r--r-- 1 root root 555 Apr 16 18:07 index.html

步骤二:备份原来默认主页并提供方一个测试页

  1. [root@node01 html]# cp index.html{,.bak}
  2. [root@node01 html]# vim index.html
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title></title>
  8. <style>
  9. @keyframes myAnimation {
  10. from {
  11. width: 100px;
  12. height: 75px;
  13. background-color: yellow;
  14. border: 1px solid red;
  15. }
  16. to {
  17. width: 200px;
  18. height: 150px;
  19. background-color: green;
  20. border: 1px solid red;
  21. }
  22. }
  23. div {
  24. animation-name: myAnimation;
  25. transition-duration: 1s;
  26. transition-timing-function: ease;
  27. animation-iteration-count: infinite;
  28. animation-play-state: running;
  29. animation-direction: reverse;
  30. animation: myAnimation 10s infinite linear;
  31. }
  32. div:hover {
  33. display: none;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div>
  39. </div>
  40. </body>
  41. </html>

步骤三:配置文件添加虚拟主机部分

  1. [root@node01 conf]# pwd
  2. /usr/local/src/nginx/conf
  3. [root@node01 conf]# vim nginx.conf
  4. server {
  5. listen 80;
  6. server_name bbs.openlab.edu;
  7. location / {
  8. root html/bbs;
  9. index index.html index.htm; # 可以修改优先级,先访问的可以放前边
  10. }
  11. }
  12. server {
  13. listen 80;
  14. server_name blog.openlab.edu;
  15. location / {
  16. root html/blog;
  17. index index.html index.htm;
  18. }
  19. }

步骤四:没有做DNS服务,就配置一个hosts解析

PS:用win上的浏览器测试,也要在win的hosts文件中添加
路径:C:\Windows\System32\drivers\etc

  1. [root@node01 conf]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.11.110 bbs.openlab.edu blog.openlab.ed

步骤五:准备默认主页

  1. [root@node01 html]# for name in blog bbs;do mkdir $name;done
  2. [root@node01 html]# for name in blog bbs ;do echo " $name test" > $name/index.html ;done

步骤六:重启服务测试

  1. [root@node01 conf]# curl http://bbs.openlab.edu
  2. bbs test
  3. [root@node01 conf]# curl http://blog.openlab.edu
  4. blog test

3、配置nginx基于用户和地址的访问控制

禁止某个ip或者一个ip段访问.如果指定unix:,那将禁止socket的访问.注意:unix在1.5.1中新加入的功能,如果你的版本比这个低,请不要使用这个方法

基于地址访问控制

  1. server {
  2. listen 192.168.11.110:80;
  3. server_name bbs.openlab.edu;
  4. location / {
  5. autoindex on;
  6. root html/bbs;
  7. index index.html index.htm;
  8. deny 192.168.11.111;
  9. allow 192.168.11.0/24;
  10. deny all;
  11. }
  12. location /nginx_status {
  13. stub_status on;
  14. access_log off;
  15. }
  16. }

重启服务并测试:

允许通过的网段:

  1. [root@template ~]# ifconfig
  2. ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  3. inet 192.168.11.10 netmask 255.255.255.0 broadcast 192.168.11.255
  4. inet6 fe80::23ff:1697:647:7139 prefixlen 64 scopeid 0x20<link>
  5. ether 00:0c:29:bc:8b:08 txqueuelen 1000 (Ethernet)
  6. RX packets 589 bytes 49970 (48.7 KiB)
  7. RX errors 0 dropped 0 overruns 0 frame 0
  8. TX packets 968 bytes 115511 (112.8 KiB)
  9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  10. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  11. inet 127.0.0.1 netmask 255.0.0.0
  12. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  13. loop txqueuelen 1000 (Local Loopback)
  14. RX packets 0 bytes 0 (0.0 B)
  15. RX errors 0 dropped 0 overruns 0 frame 0
  16. TX packets 0 bytes 0 (0.0 B)
  17. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  18. [root@template ~]# curl http://bbs.openlab.edu
  19. bbs test

拒绝的地址:

  1. [root@node02 ~]# ifconfig
  2. ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  3. inet 192.168.11.111 netmask 255.255.255.0 broadcast 192.168.11.255
  4. inet6 fe80::de65:5eb0:ef21:bfad prefixlen 64 scopeid 0x20<link>
  5. inet6 fe80::e8bb:875c:36dc:9aac prefixlen 64 scopeid 0x20<link>
  6. ether 00:0c:29:b0:1e:37 txqueuelen 1000 (Ethernet)
  7. RX packets 705 bytes 60926 (59.4 KiB)
  8. RX errors 0 dropped 0 overruns 0 frame 0
  9. TX packets 1180 bytes 141313 (138.0 KiB)
  10. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  11. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  12. inet 127.0.0.1 netmask 255.0.0.0
  13. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  14. loop txqueuelen 1000 (Local Loopback)
  15. RX packets 0 bytes 0 (0.0 B)
  16. RX errors 0 dropped 0 overruns 0 frame 0
  17. TX packets 0 bytes 0 (0.0 B)
  18. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  19. [root@node02 ~]# curl -I http://blog.openlab.edu
  20. HTTP/1.1 403 Forbidden
  21. Server: Tengine/2.2.0
  22. Date: Sun, 16 Apr 2023 11:45:53 GMT
  23. Content-Type: text/html
  24. Content-Length: 589
  25. Connection: keep-alive

基于用户控制
对于实现访问网站或目录密码认证保护,nginx的HTTP基本认证模块(HTTP Auth Basic)可以实现。这个模块提供基于用户名与密码的验证来保护你的站点或站点的一部分

  1. # 在location中添加这俩行
  2. auth_basic "Restricted";
  3. auth_basic_user_file /usr/local/nginx/webpass;
  4. server {
  5. listen 80;
  6. server_name bbs.openlab.edu;
  7. location / {
  8. root html/bbs;
  9. index index.html index.htm;
  10. auth_basic "Restricted";
  11. auth_basic_user_file /usr/local/src/nginx/webpass;
  12. }
  13. }

auth_basic
指令包含一个具有测试用户名和密码的HTTP基本认证,指定的参数将用于认证域。如果将值设置为“off”
则忽略下级指令继承的动作。
auth_basic_user_file
指令为验证域指定了密码文件,0.6.7版本以后这里指定的文件是nginx.conf所在目录的相对路径,而不
是–prefix指定的路径。
“Restricted" 单词将会出现在第一次访问Nginx站点的弹出框内

创建账号密码, 此账号密码就是用户访问网站时需要输入的

  1. [root@node01 conf]# yum install httpd-tools -y

使用方法:

  1. [root@node01 conf]# htpasswd -cm /usr/local/src/nginx/webpass tom
  2. New password:
  3. Re-type new password:
  4. Adding password for user tom
  5. [root@node01 conf]# more /usr/local/src/nginx/webpass
  6. tom:$apr1$mlWgXfOz$6j4C758K/wsTDDdQtFH990

重启Nginx 使配置修改生效

浏览器测试:

  1. [root@node01 conf]# yum install elinks.x86_64 -y
  2. [root@node1 ~]# elinks http://bbs.openlab.edu/nginx_status

原文链接:https://www.cnblogs.com/sre-chan/p/17324289.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号