1、安装依赖包
yum -y install gcc pcre-devel && yum clean all
(安装过程中会提示缺少依赖包,这里统一安装)
2、安装依赖包 apr、apr-util
1)安装 apr
tar -zxvf apr-1.5.1.tar.gz
cd apr-1.5.1
./configure --prefix=/usr/local/apr
make && make install && make clean
2)安装 apr-util
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install && make clean
3、下载 Apache 2.4.12
wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.4.12.tar.gz
(wget 命令不存在可以先安装 yum -y install wget)
4、解压 Apache 2.4.12
tar -zxvf httpd-2.4.12.tar.gz
cd httpd-2.4.12
5、编译安装
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=prefork
make && make install && make clean
6、创建相关用户及目录
useradd -M -u 500 apache
mkdir -p /www/localhost/ /data/logs/localhost/ /usr/local/apache/logs/localhost
chown -R apache:apache /www/
7、修改配置文件 httpd.conf
vi /usr/local/apache/conf/httpd.conf
Listen 80
User apache
Group apache
ServerName 127.0.0.1:80
# ServerSignature Off
# ServerTokens Prod
<Directory />
AllowOverride none
# Require all denied
</Directory>
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
Include conf/extra/httpd-vhosts.conf
8、修改虚拟配置文件 httpd-vhosts.conf
vi /usr/local/apache/conf/extra/httpd-vhosts.conf
删除所有内容,并加入如下代码:
<VirtualHost *:80>
DocumentRoot "/www/localhost/"
ServerName localhost
CustomLog "logs/localhost/access_log" combined
ErrorLog "logs/localhost/error_log"
</VirtualHost>
9、启动、停止、重启 apache
启动
/usr/local/apache/bin/apachectl
停止
/usr/local/apache/bin/apachectl stop
重启
/usr/local/apache/bin/apachectl restart
平滑启动
/usr/local/apache/bin/apachectl -k graceful
10、启动脚本
cp /usr/local/apache/bin/apachectl /et/init.d/httpd
vi /etc/init.d/httpd
在文件开头加入下面几行: #!/bin/sh #chkconfig:2345 50 90 #description:Activates/Deactivates Apache Web Server
保存后执行如下命令
chmod +x /etc/init.d/httpd
chkconfig --add httpd
chkconfig httpd on
这样,APACHE 就会随电脑自动启动
chkconfig --list发现apache服务
启动
service httpd start
停止
service httpd stop
重启
service httpd restart
11、开放 80端口给外部访问
vi /etc/sysconfig/iptables
# 增加一行下方代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80-j ACCEPT
service iptables restart
这样即可通过 http 80 端口访问
12、测试访问
vi /data/www/localhost/index.html
This is a HTML file, test by apache.
http://IP:80/
成功输出说明服务正常
13、支持 PHP
编译 PHP 使得 Apache 能够处理 PHP,
1. 安装依赖包
yum -y install libxml2-devel && yum clean all
2. 下载压缩包php-5.5.26.tar.gz
cd php-5.5.26
3. 编译安装
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs [--enable-fpm]
(--prefix:安装所在的目录,--enable-fpm:支持 nginx,--with-apxs2:支持 apache,但 apache 必须先安装)
make && make install && make clean
安装完毕
5、拷贝一份 php.ini 配置文件到安装后的目录下并配置 php.ini
cp php.ini-production /usr/local/php/lib/php.ini
具体 php.ini 如何配置这里只列出常用的
vi /usr/local/php/lib/php.ini
short_open_tag = On
log_errors = On
ignore_repeated_errors = On
ignore_repeated_source = On
display_errors = Off
display_startup_errors = Off
post_max_size = 500M
file_uploads = On
upload_max_filesize = 490M
memory_limit = 128M
max_input_vars = 1000
expose_php = On
date.timezone = Asia/Shanghai
6、添加启动 php 的用户
useradd -M -u 500 apache
编译 PHP 支持 Apache 后,修改 httpd.conf 配置文件
vi /usr/local/apache/conf/httpd.conf
LoadModule php5_module modules/libphp5.so
<IfModule mime_module>
# ... 其它内容
AddType application/x-httpd-php .php .phtml
# ...其它内容
</IfModule>
14、测试访问 php
vi /www/local/index.php
<?php
echo 'This is a PHP file, test by apache';
http://IP:8080/
成功输出说明 Apache 与 PHP 连接正常