apache服务器的配置

1. apache的配置

1.1 安装apache2服务

yum install httpd -y

1.2 关闭防火墙

编辑sellinux配置文件:vim /etc/selinux/config
enforcing改成disabled
如下:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

重启主机

1.3 配置用户个人网站

1.3.1 使能用户目录

编辑apache配置文件userdir.conf

vim /etc/httpd/conf.d/userdir.conf

如下所示

<IfModule mod_userdir.c>
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
    #UserDir disabled

    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disabled" line above, and uncomment
    # the following line instead:
    # 
    UserDir public_html
</IfModule>

1.3.2 新建用户

useradd teacher1
su - teacher1
mkdir public_html
echo "Teacher1's site" > public_html/index.html
chmod o+x /home/teacher1/
# 回到root用户
exit

1.3.4 重启apache服务

systemctl restart httpd

1.3.5 测试

打开REHL服务器上的Firefox浏览器,
输入http://127.0.0.1/~teacher1
回车
在这里插入图片描述

1.4 配置apache路径别名和用户认证

1.4.1 编辑apache服务器配置文件

vim /etc/httpd/conf.d/userdir.conf

在末尾添加

Alias /private "/data/private"
<Directory "/data/private">
AllowOverride AuthConfig
Options MultiViews Indexes FollowSymLinks
Require user kingma
Authname "private"
authtype basic
authuserfile "/etc/httpd/conf/htpasswd"
</Directory>

添加文件夹
mkdir -p /data/private
写入文件到index.html
echo "Private site" > /data/private/index.html
设置账户密码
htpasswd -c -m /etc/httpd/conf/htpasswd kingma
重启httpd服务:systemctl restart httpd

1.4.2 验证

打开REHL服务器上的Firefox浏览器,
输入http://127.0.0.1/private
回车会提示要输入用户名和密码
在这里插入图片描述

1.5 路径别名+IP访问控制

编辑

vim /etc/httpd/conf.d/userdir.conf

添加

Alias /private1 "/dir1/test"
<Directory "/dir1/test/">
Options MultiViews Indexes FollowSymLinks
AllowOverride none
Require all granted
Order Allow,Deny
Allow from 127.0.0.1
</Directory>
mkdir -p /dir1/test
 echo "ip acl" > /dir1/test/index.html
systemctl restart httpd

测试 可更改Allow from 192.168.x.66测试 当为192.168.x.66时,服务器浏览器会提示无权限 当为127.0.0.1时,正常访问

在客户端输入
在这里插入图片描述

http://127.0.0.1/private1/

可以访问,但在其他主机访问,则访问失败
在这里插入图片描述

1.6 基于IP虚拟主机

这个文件默认没有 之间创建编辑就行
编辑

vim /etc/httpd/conf.d/vhost.conf

添加

<Virtualhost 172.16.12.129>
DocumentRoot "/var/www/html1"
</virtualhost>
<Virtualhost 192.168.17.3>
DocumentRoot "/var/www/ip3"
</virtualhost>
mkdir -p /var/www/html1 
echo "hello world html1" > /var/www/html1/index.html
mkdir -p /var/www/html2
echo "hello world html2" > /var/www/html2/index.html

验证

在这里插入图片描述
在这里插入图片描述

1.7 基于域名的虚拟主机

# 创建web根目录
mkdir -p /var/www/mlx
# 创建index.html文件,并写入数据
 echo "mlx site" > /var/www/mlx/index.html
 # 创建web根目录
 mkdir -p /var/www/king
 # 创建index.html文件,并写入数据
 echo "king site" > /var/www/king/index.html

vim /etc/httpd/conf.d/vhost.conf

添加
把上一步的内容删除或者注释

<Virtualhost 192.168.17.2>
DocumentRoot "/var/www/mlx"
ServerName www.mlx.com
</virtualhost>
<Virtualhost 192.168.17.2>
DocumentRoot "/var/www/king"
ServerName www.king.com
</virtualhost>

重启apache服务

systemctl restart httpd

编辑客户端hosts文件

vim /etc/hosts
192.168.17.2 www.mlx.com
192.168.17.2 www.king.com

验证

在这里插入图片描述
在这里插入图片描述

Apache服务器配置分为以下几个步骤: 1. 安装Apache服务器:在Linux系统上,可以通过包管理器安装Apache服务器,例如在Ubuntu系统上可以通过apt-get命令进行安装:`sudo apt-get install apache2` 2. 配置虚拟主机:Apache服务器可以配置多个虚拟主机,每个虚拟主机可以有自己的域名和网站根目录。在Ubuntu系统上,可以在`/etc/apache2/sites-available`目录下创建一个新的配置文件,例如`mywebsite.conf`,并按照以下格式进行配置: ``` <VirtualHost *:80> ServerName mywebsite.com DocumentRoot /var/www/mywebsite <Directory /var/www/mywebsite> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog /var/log/apache2/mywebsite_error.log CustomLog /var/log/apache2/mywebsite_access.log combined </VirtualHost> ``` 其中,`ServerName`指定虚拟主机的域名,`DocumentRoot`指定网站根目录,`<Directory>`标签内的内容指定了访问权限和配置项,`ErrorLog`指定错误日志文件的路径,`CustomLog`指定访问日志文件的路径。 3. 启用虚拟主机:在Ubuntu系统上,可以使用`a2ensite`命令启用一个虚拟主机配置文件:`sudo a2ensite mywebsite.conf` 4. 配置SSL:如果需要启用HTTPS协议,需要配置SSL证书。在Ubuntu系统上,可以使用`certbot`工具申请Let's Encrypt免费SSL证书:`sudo certbot --apache` 以上是Apache服务器配置的基本步骤,根据具体需求可以进行更详细的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值