web服务器安装与基本操作(apache)

序号主机名称系统备注
1

192.168.3.81

192.168.8.81

stu1

stua

rockylinux8.6最小化安装,关闭selinux
2

192.168.3.82

192.168.8.82

stu2

stub

rockylinux8.6最小化安装,关闭selinux

1、安装

dnf install -y httpd vim net-tools bash-completion
echo 'my first website'  > /var/www/html/index.html
systemctl enable  --now httpd
netstat -anplut| grep httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --reload

bash-completion命令补全,需重新登陆

2、基于域名的虚拟主机

虚拟主机模板目录     /usr/share/doc/httpd/httpd-vhosts.conf

192.168.3.81:

mkdir /var/www/web1
mkdir /var/www/web2
echo 'my web1' > /var/www/web1/index.html
echo 'my web2' > /var/www/web2/index.html



vim /etc/httpd/conf.d/web.conf

<VirtualHost *:80>
    ServerAdmin web1@example.com
    DocumentRoot "/var/www/web1"
    ServerName www.web1.com
    ServerAlias web1.com
    ErrorLog "/var/log/httpd/web1.com-error_log"
    CustomLog "/var/log/httpd/web1.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin web2@example.com
    DocumentRoot "/var/www/web2"
    ServerName www.web2.com
    ServerAlias web2.com
    ErrorLog "/var/log/httpd/web2.com-error_log"
    CustomLog "/var/log/httpd/web2.com-access_log" common
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin web@example.com
    DocumentRoot "/var/www/html"
    ServerName www.web.com
    ServerAlias web.com
    ErrorLog "/var/log/httpd/web.com-error_log"
    CustomLog "/var/log/httpd/web.com-access_log" common
</VirtualHost>


systemctl restart httpd

192.168.3.82:

vi /etc/hosts
192.168.3.81  stu1 www.web1.com  www.web2.com www.web.com

3、基于IP的虚拟主机

192.168.3.81

vim /etc/httpd/conf.d/web.conf

<VirtualHost 192.168.3.81:80>
    ServerAdmin web1@example.com
    DocumentRoot "/var/www/web1"
    ServerName www.web1.com
    ServerAlias web1.com
    ErrorLog "/var/log/httpd/web1.com-error_log"
    CustomLog "/var/log/httpd/web1.com-access_log" common
</VirtualHost>

<VirtualHost 192.168.8.81:80>
    ServerAdmin web2@example.com
    DocumentRoot "/var/www/web2"
    ServerName www.web2.com
    ServerAlias web2.com
    ErrorLog "/var/log/httpd/web2.com-error_log"
    CustomLog "/var/log/httpd/web2.com-access_log" common
</VirtualHost>


systemctl restart httpd

 192.168.3.82:

 4、基于端口的虚拟主机

192.168.3.81:

firewall-cmd --permanent --add-port=88/tcp
firewall-cmd --permanent --add-port=81/tcp
firewall-cmd --reload


vim /etc/httpd/conf/httpd.conf  (增加)
Listen 88
Listen 81

vim /etc/httpd/conf.d/web.conf
<VirtualHost *:81>
    ServerAdmin web@example.com
    DocumentRoot "/var/www/web1"
    ServerName www.web.com
    ServerAlias web.com
    ErrorLog "/var/log/httpd/web1.com-error_log"
    CustomLog "/var/log/httpd/web1.com-access_log" common
</VirtualHost>

<VirtualHost *:88>
    ServerAdmin web@example.com
    DocumentRoot "/var/www/web2"
    ServerName www.web.com
    ServerAlias web.com
    ErrorLog "/var/log/httpd/web2.com-error_log"
    CustomLog "/var/log/httpd/web2.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin web@example.com
    DocumentRoot "/var/www/html"
    ServerName www.web.com
    ServerAlias web.com
    ErrorLog "/var/log/httpd/web.com-error_log"
    CustomLog "/var/log/httpd/web.com-access_log" common
</VirtualHost>

5、拒绝访问

192.168.3.81:

vim /etc/httpd/conf.d/web.conf
<VirtualHost *:80>
    ServerAdmin web1@example.com
    DocumentRoot "/var/www/web1"
    ServerName www.web1.com
    ServerAlias web1.com
    ErrorLog "/var/log/httpd/web1.com-error_log"
    CustomLog "/var/log/httpd/web1.com-access_log" common

<Directory "/var/www/web1">
    order allow,deny
    allow from all
    deny from 192.168.3
</Directory>

</VirtualHost>

<VirtualHost *:80>
    ServerAdmin web2@example.com
    DocumentRoot "/var/www/web2"
    ServerName www.web2.com
    ServerAlias web2.com
    ErrorLog "/var/log/httpd/web2.com-error_log"
    CustomLog "/var/log/httpd/web2.com-access_log" common

<Directory "/var/www/web2">
    order deny,allow
    allow from all
    deny from 192.168.3.82
</Directory>

</VirtualHost>


<VirtualHost *:80>
    ServerAdmin web@example.com
    DocumentRoot "/var/www/html"
    ServerName www.web.com
    ServerAlias web.com
    ErrorLog "/var/log/httpd/web.com-error_log"
    CustomLog "/var/log/httpd/web.com-access_log" common

<Directory "/var/www/html">
    order allow,deny
    deny from 192.168.3.82
    allow from all
</Directory>

</VirtualHost>

systemctl restart httpd

192.168.3.82:

 注:网段为192.168.3

Order deny,allow
 allow from all
 deny from 192.168.3.82
 #全部都可以访问
-------------------------------
Order allow,deny
 deny from 192.168.3.82
 allow from all
 #只有192.168.3.82不能访问
-------------------------------
Order allow,deny
 allow from all
 deny from 192.168.3.82
 #只有192.168.3.82不能访问
-------------------------------
Order allow,deny
 deny from all
 allow from 192.168.3
 #全部都不能访问
-------------------------------
Order allow,deny
 allow from 192.168.3
 deny from all
 #全部都不能访问 
-------------------------------

6、用户名密码访问网站

192.168.3.81

<VirtualHost *:80>
    ServerAdmin web@example.com
    DocumentRoot "/var/www/html"
    ServerName 192.168.3.81
    ServerAlias web.com
    ErrorLog "/var/log/httpd/web.com-error_log"
    CustomLog "/var/log/httpd/web.com-access_log" common

<Directory "/var/www/html">
    AllowOverride None
    authtype basic
    authname "my website"
    authuserfile /etc/httpd/conf/passwd.secret
    require valid-user
</Directory>

</VirtualHost>
htpasswd -cm /etc/httpd/conf/passwd.secret web

systemctl restart httpd

windows系统访问

http://192.168.3.81

​​​​​​​ 




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值