记一次apache共用80端口多网站配置踩坑

By | 7月 23, 2019

近期拿到一台服务器要帮R童鞋搭建跑一个网站,除了服务器的地址和用户名密码信息,其他一切都为未知,R童鞋对这个也不清楚,所以只能靠自己摸索了。 ❓

未知是最麻烦的,只能靠自己一步步探索,当然也更具趣味性和挑战性。做事第一步,梳理需求和现状。

需求:在一台服务器上搭建一个网站可以正常运行。

现状:一台情况未知的CentOs7.x服务器,一个未解析的域名,几个UI页面,其他???。

首先,我在浏览器通过IP访问该服务器,发现有一个可以访问的网站,80端口被占用。于是在服务器上查是什么服务占用了80端口

[root@VM_0_13_centos ~]# ss -lntpd |grep 80
tcp    LISTEN     0      128       *:80                    *:*                   
users:(("httpd",pid=27481,fd=3),("httpd",pid=25965,fd=3),("httpd",pid=25964,fd=3),("httpd",pid=25963,fd=3),
("httpd",pid=25962,fd=3),("httpd",pid=25961,fd=3),("httpd",pid=24597,fd=3),("httpd",pid=8298,fd=3),
("httpd",pid=4712,fd=3),("httpd",pid=4709,fd=3),("httpd",pid=4707,fd=3))

,发现是httpd,因为一零在服务器上通常使用nginx服务器,本地测试使用Apache,所以先入为主去一看不是Nginx服务就通过find命令找Nginx位置及配置文件,结果后面绕了一圈后又回到原点。

因为80端口配置了一个服务并且不是Nginx,一零首先想到的是用Nginx在重新开的一个端口上运行。

第一步,修改Nginx的配置文件,Nginx的默认安装的配置文件位置是/etc/nginx/nginx.conf,这里没有新增额外的配置文件,直接对默认配置文件中的server块进行修改配置。

server {
        listen       默认端口80(改)  default_server;
        listen       [::]:默认端口80(改) default_server;
        server_name  _;  #默认域名
        root         /var/www/html;   #网站目录,修改为自己的

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

第二步,将写好的网页放置到网站根目录,具体细节这里不详细描述,这属于开发部分。

第三步,启动Nginx,因为是7.x版本的系统,跟6.x不同,具体如下:

[root@VM_0_13_centos nginx]# systemctl start nginx.service
[root@VM_0_13_centos nginx]# systemctl status nginx.service
[root@VM_0_13_centos nginx]# systemctl stop nginx.service

然后在浏览器通过ip+端口或者域名+端口的访问即可访问网站。

然鹅,因为R童鞋只想通过域名不加端口的方式访问,因为默认域名映射的是80端口,于是一零想到了采用隐性URL的方式,发现竟然需要付费,想想还是去改现有的服务器配置好了。

又一次打开了的Google,看已经在跑的网站,通过Wappalyzer这个小工具,我发现了我一开始忽视了地方,应用服务器使用的是Apache2.4.6,真是好气。于是开始直接配置Apache仅仅一个80端口支持多个域名访问,Apache的配置文件为/etc/httpd/conf/httpd.conf,值需要在最后增加如下内容即可:

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName www.onezero.cc  #域名
  DocumentRoot /website      #网站目录
</VirtualHost>

紧接着停止重启服务,这样就可以一个端口解析不同的域名。

[root@VM_0_13_centos conf]# systemctl stop httpd.service
[root@VM_0_13_centos conf]# systemctl start httpd.service
[root@VM_0_13_centos conf]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-07-22 16:11:24 GMT; 4s ago

然鹅,事情并没有完,当开开心心的打开浏览器通过域名访问时,却发现

You don't have permission to access / on this server.

这个也比较好解决,因为默认设置了禁止对根目录进行访问,只需要配置你的网站根目录访问权限即可,配置的位置在之前的配置文件httpd.conf。

<Directory "/website">
    AllowOverride None
    Require all granted
</Directory>

然后重启Apache,就可以正常访问网站啦!

发表评论

您的电子邮箱地址不会被公开。