linux Apache安装配置与使用详解

linux Apache安装配置与使用详解
1、apache下载安装
2、apache配置虚拟主机(多站点)

一、下载安装
1 安装前的准备工作
检查该环境中是否已经存在httpd服务的配置文件,默认存储路径:/etc/httpd/httpd.conf。如果已经存在/etc/httpd/httpd.conf,请先卸载或者关闭centos系统自带的web服务,执行命令:chkconfig httpd off,或者把centos自带的httpd服务的80端口改为其他端口,只要不与要安装的Apache服务的端口冲突就可以。
停止并卸载Linux系统自带的httpd服务,执行下面的命令:
service httpd stop
ps -ef | grep httpd
kill -9 pid号(逐个删除)
rpm -qa |grep httpd
rpm -e httpd软件包
最后查看下apache默认配置文件httpd.conf是否被删除,执行下面的命令:
find / -name httpd.conf

2 下载apache源码包(httpd-2.4.28.tar.gz或httpd-2.2.34.tar.gz)
执行下面的命令:
Wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.28.tar.gz
或者
Wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.2.34.tar.gz

3 安装相关软件包apr、apr-util、pcre
在编译安装apache源码包之前,我们需要先安装三个依赖软件包apr、apr-util、pcre, 否则会出现下面的安装错误。
checking for APR… no
configure: error: APR not found. Please read the documentation.
首先执行下面的wget命令来下载依赖包的源码包:
Wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.2.tar.gzWget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.0.tar.gzWget https://sourceforge.net/settings/mirror_choices?projectname=pcre&filename=pcre2/10.30/pcre2-10.30.zip
执行下面的命令,编译安装相应软件包:
a)解决apr not found问题

tar -zxf apr-1.6.2.tar.gz

./configure --prefix=/usr/local/apr

make

make install

b)解决APR-util not found问题

tar -zxf apr-util-1.6.0.tar.gz

./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr-1-config

make

make install

c)解决pcre-config for libpcre not found问题

unzip pcre2-10.30.zip

cd pcre2-10.30

./configure --prefix=/usr/local/pcre

make

make install

4 安装apache 源码包
在安装Apache时,我分别针对不同版本进行了安装,在编译时是不同的,configure后跟的参数不同。
对于httpd-2.2.34版本编译命令如下:
./configure --prefix=/usr/local/apache2 (安装目录参数后面可以不加任何参数,直接安装即可)
make
make install
对于httpd-2.4.28版本编译安装命令如下:
./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre (除了指定Apache的安装目录外,还要安装apr、apr-util、pcre,并指定参数)
make
make install

5 启动apache 服务
执行下面的命令:

/usr/local/apache2/bin/apachectl start

另外如果想停止或者重启apache服务,可以执行下面的命令:
#/usr/local/apache2/bin/apachectl stop

/usr/local/apache2/bin/apachectl restart

网站的php程序文件放在/usr/local/apache2/htdocs目录下,下面应该会有一个名为index.php的文件。

6 验证apache服务器是否配置成功
通过浏览器打开连接http://localhost:80, 如果看到页面中显示“It works!”字样,则代表Apache验证通过。如果网站的index后缀是PHP格式的,则要修改httpd.conf配置文件(/usr/local/apache2/conf),在DirectoryIndex增加 index.php。

DirectoryIndex: sets the file that Apache will serve if a directory

is requested.

DirectoryIndex index.html index.php 如果网站不能访问,那么可能是防火墙开启导致,执行下面的命令,将防火墙关闭,或者开启80端口。 执行下面命令:

service iptables stop

二、Apache 配置虚拟主机(多站点)
Apache配置虚拟主机的三种方式:基于IP, 基于主机名和基于端口。
一、基于IP
1 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP:

ifconfig eth0:1 192.168.1.11

ifconfig eth0:2 192.168.1.12

ifconfig eth0:3 192.168.1.13

2 修改hosts文件,添加三个域名与之一一对应:
192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com

3 建立虚拟主机存放网页的根目录,如在/www目录下建立test1、test2、test3文件夹,其中分别存放1.html、2.html、3.html
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
4 在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着在httpd-vhosts.conf中写入如下配置:

ServerName www.test1.com
DocumentRoot /www/test1/

Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow From All



ServerName www.test1.com
DocumentRoot /www/test2/

Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow From All



ServerName www.test1.com
DocumentRoot /www/test3/

Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow From All

5 测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com
二、基于主机名
1 设置域名映射同一个IP,修改hosts:
192.168.1.10 www.test1.com
192.168.1.10 www.test2.com
192.168.1.10 www.test3.com

2 跟上面一样,建立虚拟主机存放网页的根目录
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

3 在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着在httpd-vhosts.conf中写入如下配置:
为了使用基于域名的虚拟主机,必须指定服务器IP地址(和可能的端口)来使主机接受请求。可以用NameVirtualHost指令来进行配置。 如果服务器上所有的IP地址都会用到, 你可以用*作为NameVirtualHost的参数。在NameVirtualHost指令中指明IP地址并不会使服务器自动侦听那个IP地址。 这里设定的IP地址必须对应服务器上的一个网络接口。
下一步就是为你建立的每个虚拟主机设定配置块,的参数与NameVirtualHost指令的参数是一样的。每个定义块中,至少都会有一个ServerName指令来指定伺服哪个主机和一个DocumentRoot指令来说明这个主机的内容存在于文件系统的什么地方。
如果在现有的web服务器上增加虚拟主机,必须也为现存的主机建造一个定义块。其中ServerName和DocumentRoot所包含的内容应该与全局的保持一致,且要放在配置文件的最前面,扮演默认主机的角色。

NameVirtualHost *:80

ServerName *
DocumentRoot /www/


ServerName www.test1.com
DocumentRoot /www/test1/

Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all



ServerName www.test2.com
DocumentRoot /www/test2/

Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all



ServerName www.test3.com
DocumentRoot /www/test3/

Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all


4 我们来测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com
三、基于端口
1 修改配置文件
将原来的
Listen 80
改为
Listen 80
Listen 8080

2 更改虚拟主机设置:

DocumentRoot /var/www/test1/
ServerName www.test1.com


DocumentRoot /var/www/test2
ServerName www.test2.com

你可能感兴趣的:(linux Apache安装配置与使用详解)