Apache部署与虚拟主机
资源列表
操作系统 |
配置 |
主机 |
IP |
CentOS7.3.1611 |
2C4G |
apache |
192.168.72.154 |
基础环境
python
复制代码
systemctl stop firewalld
systemctl disable firewalld
python
复制代码
setenforce 0
sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
python
复制代码
hostnamectl set-hostname apache
一、安装依赖
python
复制代码
yum -y install gcc gcc-* make
yum -y install apr-util-devel pcre-devel zlib-devel
二、源码编译及安装
1、解包
python
复制代码
tar zxf httpd-2.4.25.tar.gz -C /usr/src
cd /usr/src/httpd-2.4.25/
2、配置编译安装
python
复制代码
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
# --prefix:指定将 httpd 服务程序安装到哪个目录下,如/usr/local/httpd
# --enable-so:启用动态加载模块支持,使 httpd 具备进一步扩展功能的能力。
# --enable-rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护。
# --enable-charset-lite:启用字符集支持,以便支持使用各种字符集编码的网页。
# --enable-cgi:启用 CGI 脚本程序支持,便于扩展网站的应用访问能力。
make && make install
3、优化执行路径
- 通过源码编译安装的 httpd 服务,程序路径并不在系统默认的搜索路径中,为了使 该服务在使用时更加方便,可以为相关程序添加符号链接
python
复制代码
ln -s /usr/local/httpd/bin/* /usr/local/bin
ls -l /usr/local/bin/httpd /usr/local/bin/apachectl
4、添加为系统服务
- 在/lib/systemd/system/目录下,建立一个以.service 结尾的单元(unit)配置文件, 用于控制由 Systemd 管理或监控的 httpd 服务
python
复制代码
cat > /lib/systemd/system/httpd.service << 'EOF'
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl start httod.service
systemctl enable httpd.service
systemctl is-enabled httpd.service
三、虚拟主机
1、基于域名的虚拟主机
python
复制代码
# 为虚拟主机准备网页文档
mkdir -p /var/www/html/kgccom
mkdir -p /var/www/html/kccecom
echo "<h1>www.kgc.com</h1>"> /var/www/html/kgccom/index.html
echo "<h1>www.kcce.com</h1>"> /var/www/html/kccecom/index.html
2、添加虚拟主机配置
python
复制代码
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
//创建独立的配置文件
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
ErrorLog "logs/www.kcce.com.error_log"
CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
//加载独立的配置文件
vi /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf
# 重启服务
systemctl restart httpd
3、基于 IP 地址、基于端口的虚拟主机
python
复制代码
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
//创建独立的配置文件
<VirtualHost 192.168.72.154:80>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.72.154:80>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
ErrorLog "logs/www.kcce.com.error_log"
CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
python
复制代码
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
//创建独立的配置文件
<VirtualHost 192.168.72.154:81>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.72.154:82>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
ErrorLog "logs/www.kcce.com.error_log"
CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
# 还需要再主配置文件添加 Listen 进行监听端口