Apache部署与虚拟主机

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 地址、基于端口的虚拟主机

  • 基于 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 进行监听端口
相关推荐
小小龙学IT5 天前
Apache Airflow 2.x 深度指南:用 Python 编排一切的现代化工作流引擎
开发语言·python·apache
Shepherd06195 天前
【IT 运维】Apache 使用 mod_remoteip 恢复 Cloudflare 后的真实访客 IP
运维·tcp/ip·apache
isyangli_blog5 天前
SDN 基本应用实践 —— 使用命令行实现简易防火墙功能实验报告
服务器·php·apache
小小龙学IT6 天前
Apache Pulsar 深度解析:从架构设计到生产落地
apache
Full Stack Developme7 天前
Apache Tika 教程
java·开发语言·python·apache
laplaya7 天前
C++大型项目组件通信与依赖管理实践
c++·log4j·apache
万岳科技8 天前
教育培训小程序如何构建线上线下一体化教学体系
小程序·apache
yyuuuzz8 天前
云服务器软件部署的几个常见问题
运维·服务器·开发语言·网络·云计算·php·apache
分布式存储与RustFS8 天前
Apache Iceberg数据湖轻量化搭建:基于Rust开源存储方案
开源·apache·iceberg·rustfs·ai存储·ai memory·s3 table
睡不醒男孩0308239 天前
中启乘数 CLup 6.x Apache Doris 存算一体集群管理技术文档
apache·doris·clup