Linux下Apache下载安装

 
 
一.    安装
1.   yum 安装
yum install httpd –y
 
2.   源码安装
1)   在官方网站(http://apache.org/dyn/closer.cgi)上下载php到本地。
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.32.tar.gz
2)   解压缩
tar –xf httpd-2.4.32.tar.gz
3)   进入目录
cd httpd-2.4.32
4)   创建文件夹
mkdir /usr/local/ apache-2.4.32
5)   安装依赖
yum install apr apr-devel apr-util apr-util-devel –y
注:一般不推荐用yum安装apr,因为apache-2.4以后需要高版本的apr支持,而yum源的apr版本很可能不够。
所以:
wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.3.tar.gz
tar -xf apr-1.6.3.tar.gz
cd apr-1.6.3
mkdir /usr/local/apr
./configure --prefix=/usr/local/apr
make && make install
wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
tar xf apr-util-1.6.1.tar.gz
mkdir /usr/local/apr-util
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
如果是新装系统,全部包如下:
yum -y install gcc
yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install bzip2
yum -y install bzip2-devel 
6)   安装
./configure --prefix=/usr/local/apache-2.4.32 --sysconfdir=/etc/httpd --enable-so --enable-rewirte --enable-ssl --enable-cgi --enable-cgid--enable-modules=most --enable-modules-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util
--prefix=/usr/local/apache-2.4.32 :指定安装目标路径
--sysconfdir=/etc/httpd :指定配置文件安装位置
--enable-so :支持动态共享模块,如果没有这个模块PHP将无法与apache结合工作
--enable-rewirte :支持URL重写
--enable-ssl :启用支持ssl
--enable-cgi :启用支持cgi
--enable-cgid :启用支持带线状图形的CGI脚本 MPMs
--enable-modules=most :安装大多数模块
--enable-modules-shared=most :安装大多数共享模块
--enable-mpms-shared=all :支持全部多道处理方式
--with-apr=/usr/local/apr/ :指定apr路径
--with-apr-util=/usr/local/apr-util :指定apr-util路径
make && make install
 
二.     启动和停止

1)           启动apache

 /usr/local/apache-2.4.32/bin/apachectl start

2)           停止apache

/usr/local/apache-2.4.32/bin/apachectl stop

查询apache是否启动或者停止

lsof –i :80  或者 ps –ef | grep httpd | grep –v grep

如果在启动apache的时候出现错误“AH00558,…….”

编辑 /etc/httpd/httpd.conf 配置文件,将其中 #ServerName www.example.com:80前面的注释去掉即可,或者修改为 ServerName localhost:80 并去掉#,然后重启apache。

3)           拷贝启动文件

cp /usr/local/apache-2.4.32/bin/apachectl /etc/init.d/httpd

vim /etc/init.d/httpd

在开头添加:

#!/bin/sh

#chkconfig: 234585 15

4)           添加开机启动

chkconfig --add /etc/init.d/httpd


三.  配置
    apache的默认站点目录为/usr/local/apache/htdocks,在里面新增文件夹可以分类放置不同的站点。也可以修改站点目录,在/etc/httpd/httpd.conf里面修改。
    一般来说,apache会使用vhosts来管理和创建站点,进入/etc/httpd/httpd.conf,直接查找vhost即可找到。去掉vhost的配置那一句前面的#,保存退出。
1)   在/etc/httpd/extra/里面可以找到httpd-vhosts.conf ,这是vhosts的配置文件模板。并拷贝创建3个配置文件。
cp /etc/httpd/extra/httpd-vhosts.conf /etc/httpd/extra/www-vhosts.conf
cp /etc/httpd/extra/httpd-vhosts.conf /etc/httpd/extra/bbs-vhosts.conf
cp /etc/httpd/extra/httpd-vhosts.conf /etc/httpd/extra/blog-vhosts.conf
 
2)   备份httpd-vhosts.conf
cp /etc/httpd/extra/httpd-vhosts.conf(,.backup)
3)   修改httpd-vhosts.conf
将配置文件中的 2段 VirtualHost 都删掉,添加:
include /etc/httpd/extra/www-vhosts.conf
include /etc/httpd/extra/bbs-vhosts.conf
include /etc/httpd/extra/blog-vhosts.conf
4)   修改www-vhosts.conf,bbs-vhosts.conf,blog-vhosts.conf 3个配置文件,例如:
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache-2.4.32/htdocs/www"
    ServerName www.com
    ServerAlias www.www.com
    ErrorLog "www-error_log"
    CustomLog "www-access_log" common
</VirtualHost>
5)   在/usr/local/apache-2.4.32/htdocs中创建3个文件夹
mkdir www bbs blog
6)   在各个文件夹中创建index.html
echo "This is www.com">/usr/local/apache-2.4.32/htdocs/www/index.html
echo "This is bbs.com">/usr/local/apache-2.4.32/htdocs/bbs/index.html
echo "This is blog.com">/usr/local/apache-2.4.32/htdocs/blog/index.html
7)   因为之前没有为apache分配用户,所以现在来分配用户,权限。
useradd www –s /sbin/nologin –M
chown –R www.www /usr/local/apache-2.4.32/htdocs
service httpd restart

8)   为了以后访问方便,可以做一个软连接
 ln -s /usr/local/apache-2.4.32 /usr/local/apache

四.  测试
修改客户机的hosts文件,在C:\Windows\System32\drivers\etc下。
添加 3条 DNS  ,前面是安装apache的主机IP,后面是域名(也就是www.com ,bbs.com ,blog.com )中间用空格间隔。
在浏览器中输入www.com ,测试是否会出现写有“This is www.com”的页面,如果出错,清除浏览器的缓存后再试即可。


猜你喜欢

转载自blog.csdn.net/freshair_x/article/details/80387418