web服务的部署及高级优化方案

本文详细描述了如何在Linux服务器上搭建Web服务器,包括安装Nginx,设置默认访问目录,配置SSL以实现加密访问,以及管理虚拟主机和修改hosts文件的过程。
摘要由CSDN通过智能技术生成

搭建web服务器要求如下:
1.web服务器的主机ip:172.25.254.100
[dyj@server100 ~]$ ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 172.25.254.255
......


2.web服务器的默认访问目录为/var/www/html
默认发布内容为default‘s page
第一步:
安装nginx
[root@server100 ~]# dnf install nginx.x86_64 -y
......
已安装:
  nginx-1:1.20.1-14.el9_2.1.x86_64
  nginx-core-1:1.20.1-14.el9_2.1.x86_64
  nginx-filesystem-1:1.20.1-14.el9_2.1.noarch
  redhat-logos-httpd-90.4-2.el9.noarch

完毕!
第二步:查看nginx下的文件
[root@server100 ~]# tree /etc/nginx/
/etc/nginx/
├── conf.d
├── default.d
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types
├── mime.types.default
├── nginx.conf
├── nginx.conf.default
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf

2 directories, 15 files
第三步:运行nginx
[root@server100 ~]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

第四步:查看nginx默认端口
[root@server100 ~]# netstat -antlupe | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          82656      35638/nginx: master
tcp6       0      0 :::80                   :::*                    LISTEN      0          82657      35638/nginx: master

第五步:改变默认文件目录并添加内容
[root@server100 ~]# cd /etc/nginx
[root@server100 nginx]# vim nginx.conf
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/www/html;
[root@server100 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@server100 nginx]# systemctl restart nginx.service
[root@server100 nginx]# mkdir -p  /usr/www/html
[root@server100 nginx]# echo default‘s page > /usr/www/html/index.html

测试:
[root@server100 nginx]# curl 172.25.254.100
default‘s page


3.站点news.timinglee.org默认发布目录为/var/www/virtual/timinglee.org/news
默认发布内容为 news.timinglee.org
 [root@server100 conf.d]# vim /etc/nginx/nginx.conf
include /etc/nginx/conf.d/*.conf;     ##优先级放到最后

[root@server100 conf.d]# vim vhosts.conf
server {
        listen 80;
        server_name news.timinglee.org;
        root /var/www/virtual/timinglee.org/news;
        index index.html;
}
[root@server100 conf.d]# systemctl restart nginx.service                     
[root@server100 conf.d]# mkdir -p /var/www/virtual/timinglee.org/news        [root@server100 conf.d]# echo news.timinglee.org >/var/www/virtual/timinglee.org/news/index.html
 [root@server100 conf.d]# vim /etc/hosts  
172.25.254.100 server100.timinglee.org news.timinglee.org 

测试:
[root@server100 conf.d]# curl news.timinglee.org
news.timinglee.org

4.站点login.timinglee.org在被访问时必须强制走加密协议,其默认发布目录为/var/www/virtual/timinglee.org/login
默认发布内容为login.timinglee.org
                 
[root@server100 conf.d]# mkdir -p /var/www/virtual/timinglee.org/login
[root@server100 conf.d]# echo login.timinglee.org >/var/www/virtual/timinglee.org/login/index.html
[root@server100 conf.d]# vim /etc/hosts  
172.25.254.100 server100.timinglee.org news.timinglee.org login.timinglee.org
[root@server100 conf.d]# curl login.timinglee.org
login.timinglee.org

[root@server100 conf.d]# cd
[root@server100 ~]# mkdir /etc/nginx/certs
[root@server100 ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/nginx/certs/timinglee.org.key -x509 -days 365 -out /etc/nginx/certs/timinglee.org.crt
.......+........+++++++++++++++++++++++++++++++++++++++++++++++++++++++.............

If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shanxi
Locality Name (eg, city) [Default City]:xian
Organization Name (eg, company) [Default Company Ltd]:timinglee
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:login.timinglee.org
Email Address []:admin@timing.org
[root@server100 ~]# ls /etc/nginx/certs/
timinglee.org.crt  timinglee.org.key

[root@server100 ~]# vim /etc/nginx/conf.d/vhosts.conf
server {
        listen 443 ssl;
        server_name login.timinglee.org;
        ssl_certificate "/etc/nginx/certs/timinglee.org.crt";
        ssl_certificate_key "/etc/nginx/certs/timinglee.org.key";
        root /var/www/virtual/timinglee.org/login;
        index index.html;
}
[root@server100 ~]#  systemctl restart nginx.service    
                 
[root@server100 ~]#  mkdir -p /var/www/virtual/timinglee.org/login
[root@server100 ~]#  echo login.timinglee.org >/var/www/virtual/timinglee.org/login/index.html

对域名解析:
通过C:\Windows\System32\drivers\etc\hosts路径打开hosts进行编辑
172.25.254.100  login.timinglee.org

强制加密:
[root@server100 ~]# vim /etc/nginx/conf.d/vhosts.conf
server {
        listen 80;
        server_name login.timinglee.org;
        rewrite ^/(.*)$ https://login.timinglee.org/$1 permanent;
}
[root@server100 ~]# systemctl restart nginx.service
 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值