一、从Apache官网下载Apache2.4最新版
下载Apache安装包到服务器,我们采用源码安装,下载地址:http://httpd.apache.org/download.cgi
#相关下载地址
https://httpd.apache.org/download.cgi
https://apr.apache.org/download.cgi
yum install gcc gcc++ pcre-devel expat-devel openssl-devel
yum install bzip2-devel libxml2-devel libmcrypt-devel
yum install gd zlib libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel openssl openssl-devel curl-devel libxslt-devel
#在用户目录
mkdir httpd
wget --no-check-certificate https://dlcdn.apache.org//apr/apr-iconv-1.2.2.tar.gz
wget --no-check-certificate https://dlcdn.apache.org//apr/apr-util-1.6.1.tar.gz
wget --no-check-certificate https://dlcdn.apache.org//apr/apr-1.7.0.tar.gz
wget --no-check-certificate https://dlcdn.apache.org//httpd/httpd-2.4.52.tar.gz
tar xvxf httpd-2.4.52.tar.gz
tar xvxf apr-1.7.0.tar.gz
tar xvxf apr-util-1.6.1.tar.gz
tar xvxf apr-iconv-1.2.2.tar.gz
mv apr-1.7.0 httpd-2.4.52/srclib/apr
mv apr-iconv-1.2.2 httpd-2.4.52/srclib/apr-iconv
mv apr-util-1.6.1 httpd-2.4.52/srclib/apr-util
cd httpd-2.4.52
./configure --prefix=/usr/local/httpd2.4.52 --sysconfdir=/etc/httpd2.4 --enable-so --enable-ssl --enable-cgi --enable-cgid --enable-rewrite --with-zlib --with-pcre --with-included-apr --with-apr-iconv=srclib/apr-iconv --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork --enable-include
make -j 8 && make install
二、安装程序依赖
若出现如下错误
checking for gcc option to enable C11 features... unsupported
checking for gcc option to enable C99 features... -std=gnu99
checking how to run the C preprocessor... gcc -E
checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
解决办法:
安装PCRE、访问http://pcre.org/ 下载到服务器
# 因为我的服务器无法从github下载文件、所以将文件存放在自己的博客上
wget --no-check-certificate https://nchc.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.zip
unzip pcre-8.45.zip
cd pcre-8.45
./configure --enable-utf8
make
make install
安装完成后再进入httpd解压目录,执行Apache编译命令
./configure --prefix=/usr/local/httpd2.4.52 --sysconfdir=/etc/httpd2.4 --enable-so --enable-ssl --enable-cgi --enable-cgid --enable-rewrite --with-zlib --with-pcre --with-included-apr --with-apr-iconv=srclib/apr-iconv --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork --enable-include
make && make install
cd /usr/local/
ln -s httpd2.4.52/ httpd
PATH=/usr/local/cmake/bin:/usr/local/httpd/bin:$PATH
httpd -v
# Server version: Apache/2.4.52 (Unix)
# Server built: Feb 21 2022 07:44:04
# You have new mail in /var/spool/mail/root
# 创建用于启动httpd的账号和组
getent group apache > /dev/null || groupadd -g 48 -r apache
getent passwd apache > /dev/null || useradd -r -u 48 -g apache -s /sbin/nologin apache
# 修改apache目录所有者和组
chown -R apache:apache /usr/local/httpd2.4.52/
# vim /etc/httpd2.4/httpd.conf
## User apache
## Group apache
apachectl=/usr/local/httpd/bin/apachectl
httpd=${HTTPD-/usr/local/httpd/bin/httpd}
prog=httpd
cp /usr/local/httpd/bin/apachectl /etc/rc.d/init.d/httpd
chkconfig --add httpd
chkconfig httpd on
service httpd start
# 开启虚拟机配置文件、将httpd.conf的Include extra/httpd-vhosts.conf前面的注释去掉
vim /etc/httpd2.4/httpd.conf
# 仿照模板修改配置
/etc/httpd2.4/extra/httpd-vhosts.conf
安装PHP
#用户目录
mkdir php5.6
wget --no-check-certificate https://www.php.net/distributions/php-5.6.40.tar.gz
tar zvxf php-5.6.40.tar.gz
cd php-5.6.40
./configure --prefix=/usr/local/php5.6 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib --with-libxml-dir --with-mysqli=mysqlnd --with-openssl --with-pcre-regex --with-pdo-mysql=mysqlnd --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-mysqlnd --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-pthreads --enable-maintainer-zts --enable-fileinfo --with-apxs2=/usr/local/httpd2.4.52/bin/apxs
make && make install
service httpd restart
修改PHP配置
修改文件上传大小限制
修改Apache配置使识别PHP
vim /etc/httpd2.4/httpd.conf
#去掉重定向的模块注释
LoadModule rewrite_module modules/mod_rewrite.so
#新增一行php5模块扩展
LoadModule php5_module modules/libphp5.so
全局添加PHP识别
AddType application/x-httpd-php .php
# 修改默认重定向配置
<Directory />
# 将AllowOverride None改为如下
AllowOverride All
Require all denied
</Directory>
Apache虚拟机配置案例
vim /etc/httpd2.4/extra/httpd-vhosts.conf
# 如果监听其他端口则配置新的监听端口如8080
Listen 8080
<VirtualHost *:8080>
ServerAdmin orhonit@163.com
DocumentRoot "/var/www/html"
ServerName test.xuyongsheng.cn
ErrorLog "logs/test.xuyongsheng.cn.cn-error_log"
CustomLog "logs/test.xuyongsheng.cn-access_log" common
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
<Directory "/var/www/html">
AllowOverride All
Options Indexes FollowSymLinks
Require all granted
</Directory>
</VirtualHost>