http基本配置
一.搭建服务
搭建静态网站——基于http协议的静态网站
服务器端:在linux上面实现网页服务器需要Apache这套服务器软件,httpd提供Apache主程序。
二.安装httpd
(1)装包httpd
[root@localhost ~]# yum install httpd -y
(2)安装完成之后,停止firewalld ,关闭selinux,下面所介绍的两条关闭代码均为临时关闭,重启系统后还是会开启:
systemctl stop firewalld
setenfore 0
查看防火墙状态和selinux状态命令分别为:
systemctl status firewalld
getenforce
1.如何创建多个ip地址
第一种:图形化界面手动修改,例如:
这里DHCP为动态分配ip地址,我们将它换位Manual静态手动配置ip地址,就可以配置多个ip地址,下面配置中第一栏为ip地址,第二栏是子网掩码,第三栏是网关,配置好后直接点击Apply应用就OK了。
第二种:用命令去配置,例如:
#nmcli connection modify ens160 ipv4.addresses 192.168.222.128/24 ipv4.gateway 192.168.222.2 ipv4.dns 8.8.8.8 ipv4.method manual connection.autoconnect yes
#nmcli connection modify ens160 +ipv4.addresses 192.168.222.131/24
#nmcli connection modify ens160 ipv4.addresses 192.168.222.130/24 ipv4.gateway 192.168.222.2 +ipv4.dns 114.114.114.114
用命令去创建时,第一个创建的ip为 192.168.222.128,网关 192.168.222.2,子网掩码8.8.8.8,第二个创建的就修改了ip地址,默认网关和子网掩码与第一个相同,第三个创建的不光修改了ip地址,还改了子网掩码(网关是相同的不可修改,子网掩码可以修改)
2.更改配置实现多个网站访问不同的资源信息
案例一:多个ip搭建多网站访问
(1)修改配置文件
#vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost 192.168.222.128:80>
DocumentRoot /www/128
ServerName 192.168.222.128
</VirtualHost>
<VirtualHost 192.168.222.130:80>
DocumentRoot /www/130
ServerName 192.168.222.128
</VirtualHost>
<VirtualHost 192.168.222.131:80>
DocumentRoot /www/131
ServerName 192.168.222.131
</VirtualHost>
<Directory /www>
AllowOverride none
Require all granted
</Directory>
(2)创建配置文件中相应的目录
mkdir /www/{128,130,131}
(3)根据配置文件创建对应的资源(文件)
[root@localhost conf.d]# echo this is 128 > /www/128/index.html
[root@localhost conf.d]# echo this is 130 > /www/130/index.html
[root@localhost conf.d]# echo this is 131 > /www/131/index.html
(4)重启测试
systemctl restart httpd
案例二:多端口访问不同的网站内容
(1)修改配置文件
#vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost 192.168.222.128:80>
DocumentRoot /www/128
ServerName 192.168.222.128
</VirtualHost>
<VirtualHost 192.168.222.128:8909>
DocumentRoot /www/8909
ServerName 192.168.222.128
</VirtualHost>
Listen 8909
<VirtualHost 192.168.222.128:10000>
DocumentRoot /www/10000
ServerName 192.168.222.128
</VirtualHost>
<Directory /www>
AllowOverride none
Require all granted
</Directory>
Listen 10000
(2)创建配置文件中相应的目录
mkdir /www/{128,8909,10000}
(3)创建文件
[root@localhost conf.d]# echo this is 128 > /www/128/index.html
[root@localhost conf.d]# echo this is 8909 > /www/8909/index.html
[root@localhost conf.d]# echo this is 10000 > /www/10000/index.html
(4)重启测试
systemctl restart httpd
案例三:多域名访问网站内容
首先我们得知道ip---------域名的关系是"一对多"的关系,那我们可以创建不同的域名去访问同一地址下不同路径的网站内容,例如:
(1)修改配置文件
#vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost 192.168.222.128:80>
DocumentRoot /www/wxy
ServerName www.wxy.com
</VirtualHost>
<VirtualHost 192.168.222.128:80>
DocumentRoot /www/chq
ServerName www.chq.com
</VirtualHost>
<Directory /www>
AllowOverride none
Require all granted
</Directory>
(2)创建配置文件中相应的目录
[root@localhost conf.d]# mkdir /www/{wxy,chq}
(3)创建文件
[root@localhost conf.d]# echo this is wxy > /www/wxy/index.html
[root@localhost conf.d]# echo this is chq > /www/chq/index.html
(4)重启测试
[root@localhost conf.d]# systemctl restart httpd
这个案例在进浏览器访问之前需要现在Windows下C:\Windows\System32\drivers\etc这个目录下hosts文件中添加你所创建的两个ip地址和对应的域名,例如:
192.168.222.128 www.wxy.com
192.168.222.128 www.chq.com
添加保存之后就可以在浏览器上通过域名访问到网站内容了
补充内容:
开启和关闭httpd
在root控制权限下,查看httpd状态:
systenctl status httpd
开启httpd:
systemctl start httpd
关闭httpd:
systemctl stop httpd
重启httpd:
systemctl restart httpd
关闭selinux有两种方法:
查看selinux状态:
getenforce
(1)永久性的
修改 /etc/selinux/config文件中的SELINUX=""为disabled,然后重启。
(2)临时性的
setenforce 0
开启和关闭防火墙:
在root控制权限下,查看防火墙状态:
systemctl status firewalld
开启防火墙:
systenctl enable firewalld
临时关闭:
systemctl stop firewalld
永久关闭:
systemctl disable firewalld