Apachehttpd服务搭建

在没有nginx之前,很多人都在使用httpd作为代理服务器,当然现在仍然有很多人使用httpd作为代理服务器,httpd是Apache第一个开源项目致使很多人都称之为Apache。

2019年11月18日Netcraft统计数据

下面我们聊一下一些基本的内容

安装

yum install -y httpd

启动

systemctl start httpd #启动 systemctl stop httpd #关闭 systemctl restart httpd #重启 systemctl status httpd #当前httpd服务状态

配置文件

/etc/httpd/conf/httpd.conf httpd -t #检测配置文件是否有错

场景配置

配置每个用户的个人网站

配置文件位置:/etc/httpd/conf.d/userdir.conf

<IfModule mod_userdir.c> UserDir disabled root #禁止root用户创建 UserDir public_html </IfModule> <Directory "/home/*/public_html"> AllowOverride FileInfo AuthConfig Limit Indexes Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require

创建用户给用户家目录所有的执行权限

访问地址:http:ip/~用户名

基于ip的虚拟主机

创建虚拟网卡

ifconfig ens32:1 192.168.217.137 netmask 255.255.255.0 ifconfig ens32:2 192.168.217.138 netmask 255.255.255.0 mkdir -p /home/wwwroot/136 mkdir -p /home/wwwroot/137 mkdir -p /home/wwwroot/138 echo "IP:192.168.217.136" > /home/wwwroot/136/index.html echo "IP:192.168.217.137" > /home/wwwroot/137/index.html echo "IP:192.168.217.138" > /home/wwwroot/138/index.html

在httpd.conf中添加如下

</Directory> </VirtualHost> <VirtualHost 192.168.217.137> DocumentRoot /home/wwwroot/137 ServerName bbs.abc.com <Directory /home/wwwroot/137 > AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.217.138> DocumentRoot /home/wwwroot/138 ServerName tech.abc.com <Directory /home/wwwroot/138 > AllowOverride None Require all granted </Directory> </VirtualHost>

基于端口

创建主页

mkdir -p /home/wwwroot/6111 mkdir -p /home/wwwroot/6222 echo "port:6111" > /home/wwwroot/6111/index.html echo "port:6222" > /home/wwwroot/6222/index.html

编写httpd.conf

#添加需要监听的端口号 Listen 80 Listen 6111 Listen 6222 ...... #修改配置文件 <VirtualHost 192.168.217.136:6111> DocumentRoot "/home/wwwroot/6111" ServerName www.abc.com <Directory /home/wwwroot/6111 > AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.217.136:6222> DocumentRoot "/home/wwwroot/6222" ServerName bbs.abc.com <Directory /home/wwwroot/6222 > AllowOverride None Require all granted </Directory> </VirtualHost>

基于域名

mkdir -p /home/wwwroot/www mkdir -p /home/wwwroot/bbs mkdir -p /home/wwwroot/tech echo "WWW.linuxprobe.com" > /home/wwwroot/www/index.html echo "BBS.linuxprobe.com" > /home/wwwroot/bbs/index.html echo "TECH.linuxprobe.com" > /home/wwwroot/tech/index.html

配置文件

<VirtualHost 192.168.217.136> DocumentRoot "/home/wwwroot/www" ServerName www.abc.com <Directory /home/wwwroot/www > AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.217.136> DocumentRoot "/home/wwwroot/bbs" ServerName bbs.abc.com <Directory /home/wwwroot/bbs > AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.217.136> DocumentRoot "/home/wwwroot/tech" ServerName tech.abc.com <Directory /home/wwwroot/tech > AllowOverride None Require all granted </Directory> </VirtualHost>