Apache搭建网站(域名和端口访问)


apache httpd
提供http服务

  配置文件路径
  /etc/httpd/

  服务路径(工作目录)
  /var/www/html

快速搭建服务
1.检查环境
getenforce
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config

setenforce 0
systemctl status firewalld

2.配置Yum源
###############
[251]
name=251
baseurl=http://172.16.105.251/base
enabled=1
gpgcheck=0
##############

3.安装服务
yum -y install httpd

4.启动服务
systemctl start httpd
systemctl enable httpd

5.添加防火墙
(1)以名称
firewall-cmd --add-service=http
firewall-cmd --add-service=http --permanent

(2)以端口
netstat -anp|grep httpd
firewall-cmd --add-port=80/tcp
firewall-cmd --add-port=80/tcp --permanent

6.浏览器访问
主机IP
firewall-cmd --add-service=http

 


httpd虚拟网站
一.按端口
0.检查环境

1.创建对应的工作目录(apache工作的目录)
mkdir -p /work/html1
mkdir -p /work/html2

2.检查端口使用
netstat -anp|grep 81
netstat -anp|grep 82

3.编辑配置文件
cd /etc/httpd/conf.d
vim ab.conf(新建一个以 .conf结尾的文件,该文件是新建网站的配置文件)
#########################
Listen 81
Listen 82

<VirtualHost *:81>
ServerName a.com
DocumentRoot /work/html1
<Directory "/work/html1">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:82>
ServerName b.com
DocumentRoot /work/html2
<Directory "/work/html2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
##########################

4.放置主页文件
echo a.com > /work/html1/index.html
echo b.com > /work/html2/index.html

5.重新启动服务
systemctl restart httpd

6.添加防火墙对应端口
firewall-cmd --add-port=81/tcp --permanent
firewall-cmd --add-port=82/tcp --permanent
firewall-cmd --reload

7.浏览器访问

二 按域名
0.检查环节
getenforce
systemctl status firewalld

1.安装服务
yum -y install httpd

2.创建服务工作目录
mkdir -p /work/html1
mkdir -p /work/html2

3.编辑配置文件
cd /etc/httpd/conf.d/
vim 321.conf(新建的网站的配置文件)
##########################
<VirtualHost *:80>
ServerName www.taobao.com   #新建网站的域名)
DocumentRoot /work/html1       # 新建网站的工作目录
<Directory "/work/html1">      #该文件名需要和DocumentRoot后的文件名相同
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:80>
ServerName www.jd.com
DocumentRoot /work/html2
<Directory "/work/html2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#######################

4.给服务添加主页
echo "我是淘宝" > /work/html1/index.html    #工作目录下建index.html文件,该文件是新建网站读取的第一个文件
echo "俺是京东" > /work/html2/index.html

5.设置dns静态解析
vim /etc/hosts
################
192.168.10.100 www.jd.com
192.168.10.100 www.taobao.com
##################

6.启动服务
systemctl restart httpd
systemctl enable httpd

7.添加防火墙端口
firewall-cmd --add-port=80/tcp
firewall-cmd --add-port=80/tcp --permanent

8.测试服务
curl www.taobao.com
curl www.jd.com

9.若要在物理机的浏览器以域名访问虚拟网站,则还应该在物理机C盘中的/Windows/system32/drivers/etc/hosts中的hosts文件中添加域名解析。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM