基于多端口的Web服务
【Centos7.4版本】
!!!测试环境我们首关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld [root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config [root@localhost ~]# setenforce 0
一、配置多端口WEB服务
1、先看一下本地网卡的信息
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cat ifcfg-ens32 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=10.0.0.129 //我本机主网卡P地址 NETMASK=255.255.255.0 GATEWAY=10.0.0.2 DNS1=8.8.8.8 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens32 UUID=e4b11756-1775-4b6a-adbf-95f3f24f941e DEVICE=ens32 ONBOOT=yes
2、安装HTTP服务,并启动服务
[root@localhost ~]# yum install -y httpd [root@localhost ~]# systemctl start httpd [root@localhost ~]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
3、创建目录,写入测试首页
[root@localhost ~]# mkdir /var/www/port8000 [root@localhost ~]# mkdir /var/www/port9000 [root@localhost ~]# echo '<h1>The port is :8000</h1>' > /var/www/port8000/index.html [root@localhost ~]# echo '<h1>The port is :9000</h1>' > /var/www/port9000/index.html
4、首先看一下配置模板
[root@localhost ~]# vim /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf .......... //上面的我省略了 <VirtualHost *:@@Port@@> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/var/log/httpd/dummy-host.example.com-error_log" CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:@@Port@@> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log" CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common </VirtualHost> //这几行是配置虚拟主机的模板
5、编辑http的文件,并添加一下内容
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf ........ Listen 8000 Listen 9000 VirtualHost 10.0.0.129:8000> Documentroot "/var/www/port8000" ErrorLog "/var/log/httpd/port8000-error_log" CustomLog "/var/log/httpd/port8000-access_log" common </VirtualHost> <VirtualHost 10.0.0.129:9000> Documentroot "/var/www/port9000" ErrorLog "/var/log/httpd/port9000-error_log" CustomLog "/var/log/httpd/port9000-access_log" common </VirtualHost> //在文件的最后添加上面的内容
6、重启HTTP服务
[root@localhost ~]# systemctl restart httpd
7、查看8000和9000端口是否起来
[root@localhost ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1005/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1387/master tcp6 0 0 :::80 :::* LISTEN 24106/httpd tcp6 0 0 :::22 :::* LISTEN 1005/sshd tcp6 0 0 ::1:25 :::* LISTEN 1387/master tcp6 0 0 :::8000 :::* LISTEN 24106/httpd tcp6 0 0 :::9000 :::* LISTEN 24106/httpd udp 0 0 0.0.0.0:68 0.0.0.0:* 10151/dhclient udp 0 0 0.0.0.0:15648 0.0.0.0:* 10151/dhclient udp 0 0 127.0.0.1:323 0.0.0.0:* 722/chronyd udp6 0 0 :::23051 :::* 10151/dhclient udp6 0 0 ::1:323 :::* 722/chronyd
8、在浏览器地址栏输入IP:9000和IP:9000