Apache安装与配置详解
安装
CentOS 7.9
源码安装
安装apr
[root@ecs ~]# yum install -y bzip2 gcc [root@ecs ~]# cd /usr/local/src && tar -jxvf apr-1.7.6.tar.bz2 && cd apr-1.7.6 [root@ecs apr-1.7.6]# ./configure --prefix=/usr/local/apr [root@ecs apr-1.7.6]# make && make install安装apr-util
[root@ecs ~]# yum install -y expat-devel [root@ecs ~]# cd /usr/local/src && tar -jxvf apr-util-1.6.5.tar.bz2 && cd apr-util-1.6.5 [root@ecs apr-util-1.6.5]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@ecs apr-util-1.6.5]# make && make install- apr-util(Apache Portable Runtime Utility)从1.6.0开始依赖Expat库(xml解析器)。
- apr-util从1.6.0开始要求gcc不能低于5.0(必须支持__has_attribute ),而CentOS 7.9默认gcc版本为4.8.5,因此必须升级gcc,否则只能安装1.6以下的版本(最高版本1.5.4)。
安装apr-util 1.5.4
[root@ecs ~]# cd /usr/local/src && tar -jxvf apr-util-1.5.4.tar.bz2 && cd apr-util-1.5.4 [root@ecs apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@ecs apr-util-1.5.4]# make && make install安装pcre
[root@ecs ~]# yum install -y gcc-c++ [root@ecs ~]# cd /usr/local/src && tar -jxvf pcre-8.45.tar.bz2 && cd pcre-8.45 [root@ecs pcre-8.45]# ./configure --prefix=/usr/local/pcre [root@ecs pcre-8.45]# make && make install [root@ecs pcre-8.45]# echo "export PATH=\$PATH:/usr/local/pcre/bin" >> /etc/profile [root@ecs pcre-8.45]# source /etc/profile需要将pcre加入PATH环境变量,否则编译apache时会因找不到
pcre-config而报错:configure: error: Did not find working script at pcre-config。pcre已停止维护,最后一个版本为8.45,2015年发布了新构版本pcre2,pcre2当前最新版为10.47。
安装openssl
[root@ecs ~]# yum install -y perl perl-IPC-Cmd perl-Time-Piece [root@ecs ~]# cd /usr/local/src && tar xzvf openssl-3.5.7.tar.gz && cd openssl-3.5.7 [root@ecs openssl-3.5.7]# ./config --prefix=/usr/local/openssl -fPIC no-gost [root@ecs openssl-3.5.7]# make depend && make installOpenSSL在3.0.0之前,版本格式为
主版本.次版本.补丁版本[字母],字母代表补丁级别。如1.0.2表示1.0.2系列的首个版本,1.0.2q则表示1.0.2系列的第17个补丁版本。从3.0.0开始改为标准语义化版本,即Major(主版本).Minor(次版本).Patch(补丁版本)。3.5系列为LTS版,此系列当前(2026年8月)最高版为:3.5.7,3.0之前的最高版为:1.1.1w。
OpenSSL 3.0之前仅依赖perl,从3.0开始除了依赖perl,还依赖IPC::Cmd和Time::Piece模块,因此必须安装perl-IPC-Cmd和perl-Time-Piece。
查看是否支持perl IPC::Cmd模块:
perl -MIPC::Cmd -e 'print "success\n"'。安装apache
[root@ecs ~]# cd /usr/local/src && tar -xvf httpd-2.4.68.tar.bz2 && cd httpd-2.4.68 [root@ecs httpd-2.4.68]# ./configure --prefix=/usr/local/apache \ --with-apr=/usr/local/apr \ --with-apr-util=/usr/local/apr-util \ --with-pcre=/usr/local/pcre \ --enable-mods-shared=most \ --enable-rewrite \ --enable-ssl \ --with-ssl=/usr/local/openssl \ --enable-mpms-shared=all \ --enable-dav [root@ecs httpd-2.4.68]# make && make install [root@ecs httpd-2.4.68]# echo "export PATH=\$PATH:/usr/local/apache/bin" >> /etc/profile [root@ecs httpd-2.4.68]# source /etc/profile--enable-dav启用dav模块,可以通过dav管理svn。--enable-mpms-shared=all会在modules目录下生成mod_mpm_prefork.so、mod_mpm_worker.so和mod_mpm_event.so三个模块文件,可在httpd.conf中通过LoadModule切换MPM(默认为event模式,查看mpm模式:apachectl -V | grep -i mpm)。
也可在编译时使用--with-mpm=指定MPM模式,有效值:prefork、worker和event,编译后不可切换。
如果使用yum方式安装apr、apr-util、pcre和openssl等依赖,则需要调整--with-apr、--with-apr-util、--with-pcre和--with-ssl的参数值:
[root@ecs ~]# yum install -y apr-devel apr-util-devel pcre-devel openssl-devel \ bzip2 gcc [root@ecs ~]# cd /usr/local/src && tar -xvf httpd-2.4.68.tar.bz2 && cd httpd-2.4.68 [root@ecs httpd-2.4.68]# ./configure --prefix=/usr/local/apache \ --with-apr=/usr \ --with-apr-util=/usr \ --with-pcre \ --enable-mods-shared=most \ --enable-rewrite \ --enable-ssl \ --with-ssl \ --enable-mpms-shared=all \ --enable-dav [root@ecs httpd-2.4.68]# make && make install [root@ecs httpd-2.4.68]# echo "export PATH=\$PATH:/usr/local/apache/bin" >> /etc/profile [root@ecs httpd-2.4.68]# source /etc/profile注册服务
[root@ecs ~]# cp -f /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd [root@ecs ~]# sed -i '/#!\/bin\/sh/a\# description: Activates/Deactivates Apache Web Server' /etc/rc.d/init.d/httpd [root@ecs ~]# sed -i '/#!\/bin\/sh/a\# chkconfig: 2345 50 90' /etc/rc.d/init.d/httpd或者:
[root@ecs ~]# cat > /usr/lib/systemd/system/httpd.service <<EOF [Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=man:httpd(8) Documentation=man:apachectl(8) [Service] Type=forking PIDFile=/usr/local/apache/logs/httpd.pid ExecStart=/usr/local/apache/bin/apachectl -k start ExecReload=/usr/local/apache/bin/apachectl -k graceful ExecStop=/usr/local/apache/bin/apachectl -k stop PrivateTmp=true Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target EOF- 源码apache默认是后台守护进程,因此用
Type=forking。 PrivateTmp=true为服务提供隔离的私有文件。
- 源码apache默认是后台守护进程,因此用
开启服务并设置开机自启动
[root@ecs ~]# systemctl enable --now httpd防火墙放行端口
[root@ecs ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent [root@ecs ~]# firewall-cmd --zone=public --add-port=443/tcp --permanent [root@ecs ~]# firewall-cmd --reload配置apache
[root@ecs ~]# groupadd www [root@ecs ~]# useradd -s /sbin/nologin www -g www [root@ecs ~]# sed -i -e "s/^#LoadModule rewrite_module/LoadModule rewrite_module/" \ -e "/^User/cUser www" \ -e "/^Group/cGroup www" \ -e "/^#ServerName/cServerName localhost:80" \ -e "s/DirectoryIndex index.html/DirectoryIndex index.html index.php/" \ -e "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" \ -e '0,/AllowOverride none/s/AllowOverride none/AllowOverride all/' \ /usr/local/apache/conf/httpd.conf
yum安装
安装httpd
[root@ecs ~]# yum install -y httpd常见目录说明
目录 说明 /etc/httpd/conf/httpd.conf主配置文件 /etc/httpd/conf.d配置片段,会被主配置文件包含 /etc/httpd/conf.modules.d/模块加载配置 /var/www/html/网站根目录 /var/log/httpd/日志目录 /usr/sbin/httpdapache主程序文件 /usr/lib64/httpd/modules/模块目录