一、Apache
1、简介
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。
2、主要特点
- 开放源代码,跨平台应用
- 支持多种网页编程语言
- 模块化设计,运行稳定,良好的安全性
3、软件版本
- 1.x
- 目前最高版本是1.3,运行稳定
- 向下兼容性好,单缺乏一些较新的功能
- 2.x
- 目前最高版本是2.4
- 具有更多的功能特性
- 与1.x相比,配置管理风格存在较大差异
二、Apache初始化
1、xftp导入Apache依赖包
2、解压文件,并移动到指定目录下(解决部分依赖关系)
tar zxvf apr-1.6.2.tar.gz
tar zxvf apr-util-1.6.0.tar.gz
tar jxvf httpd-2.4.29.tar.bz2
mv apr-1.6.2 httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
3、安装依赖软件包
yum -y install gcc gcc-c++ make pcre-devel expat-devel perl
4、configure配置并编译安装
cd httpd-2.4.29/
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
make && make install
5、修改Apache启动的脚本文件
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
vi /etc/init.d/httpd ## Apache启动的文件脚本,添加如下内容
#chkconfig: 35 85 21 ## 35级别自动运行,第85个启动,第21个关闭
#description: Apache HTTP Server
6、将httpd加入service管理器并启动服务
chkconfig --add httpd
systemctl start httpd
7、优化执行路径
ln -s /usr/local/httpd/conf/httpd.conf /etc/
8、编辑配置文件并添加本机的地址和域名
vi /etc/httpd.conf
vi /etc/hosts
9、启动服务,验证是否可以访问
systemctl restart httpd
三、Apache基于域名的虚拟主机配置
1、在opt目录下创建两个目录并提供主页
mkdir /opt/test1
mkdir /opt/test2
vi /opt/test1/index.html
<html><title>test1</title><body><h1>this is test1 web!!!</h1></body></html>
vi /opt/test2/index.html
<html><title>test2</title><body><h1>this is test2 web!!!</h1></body></html>
2、修改主配置文件
vi /etc/httpd.conf
3、修改httpd-vhosts.conf配置文件
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@test1.com
DocumentRoot "/opt/test1"
ServerName www.test1.com
ErrorLog "logs/test1.com-error_log"
CustomLog "logs/test1.com-access_log" common
</VirtualHost>
<Directory "/opt/test1">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
<VirtualHost *:80>
ServerAdmin webmaster@test2.com
DocumentRoot "/opt/test2"
ServerName www.test2.com
ErrorLog "logs/test2.com-error_log"
CustomLog "logs/test2.com-access_log" common
</VirtualHost>
<Directory "/opt/test2">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
4、修改hosts文件
vi /etc/hosts
5、重启服务,并在浏览器中验证效果
systemctl restart httpd
四、Apache基于IP地址的虚拟主机
1、在enss33中新增一个地址
ifconfig ens33:1 192.168.100.10/24
ip addr
2、修改配置文件
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.40.20:80>
ServerAdmin webmaster@test1.com
DocumentRoot "/opt/test1"
ServerName www.test1.com
ErrorLog "logs/test1.com-error_log"
CustomLog "logs/test1.com-access_log" common
</VirtualHost>
<Directory "/opt/test1">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
<VirtualHost 192.168.100.10:80>
ServerAdmin webmaster@test2.com
DocumentRoot "/opt/test2"
ServerName www.test2.com
ErrorLog "logs/test2.com-error_log"
CustomLog "logs/test2.com-access_log" common
</VirtualHost>
<Directory "/opt/test2">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
3、重启服务并验证结果
systemctl restart httpd
五、Apache基于端口号的虚拟主机配置
1、修改配置文件
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
修改端口号
2、修改主配置文件,添加8080监听端口
默认只监听一个80端口,需要添加8080端口
3、重启服务验证效果
systemctl restart httpd
查看8080端口是否开启