官网地址:http://httpd.apache.org/,这里以版本2.4.43为例
点击Download进入相关下载页面下载即可
这里使用另一种方式下载:wget下载
首先我们先下载依赖
yum groupinstall "Development Tools" -y
yum install libtool -y
yum install expat-devel pcre pcre-devel openssl-devel -y
openssl是web安全通信的基石,不安装openssl相当于我们的信息都是在裸奔
依次运行以下命令下载并解压Apache,Apr和Apr-util的源码包
源码包版本会不断升级。您可以从httpd源码安装包和apr源码安装包页面查看可用的源码包版本,并将命令中的源码包版本替换为待安装的版本。
wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.43.tar.gz
wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
把它们解压到usr/local/src目录下
tar xvf httpd-2.4.43.tar.gz -C /usr/local/src
tar xvf apr-1.6.5.tar.gz -C /usr/local/src
tar xvf apr-util-1.6.1.tar.gz -C /usr/local/src
依次运行以下命令把Apr和Apr-util的文件夹移到Apache的srclib文件夹下
cd /usr/local/src
mv apr-1.6.5 httpd-2.4.43/srclib/apr
mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
依次运行以下命令编译源码
cd /usr/local/src/httpd-2.4.43
./buildconf
./configure --prefix=/usr/local/apache2 \
--enable-ssl \
--enable-so \
--with-mpm=event \
--with-included-apr \
--enable-cgi \
--enable-rewrite \
--enable-mods-shared=most \
--enable-mpms-shared=all
make && make install
依次运行以下命令设置PATH环境变量
echo "export PATH=$PATH:/usr/local/apache2/bin" > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
运行httpd -v命令可查看Apache的版本号
Server version: Apache/2.4.43 (Unix)
Server built: Jun 11 2020 14:45:40
添加Apache的启动配置文件
1 运行vi /usr/lib/systemd/system/httpd.service命令,打开Apache的启动配置文件
2按下i键,然后在配置文件中写下如下内容
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl -k start
ExecReload=/usr/local/apache2/bin/apachectl -k graceful
ExecStop=/usr/local/apache2/bin/apachectl -k graceful-stop
PIDFile=/usr/local/apache2/logs/httpd.pid
PrivateTmp=false
[Install]
WantedBy=multi-user.target
3按下Esc键,然后输入:wq并回车以保存并关闭Apache启动配置文件
依次运行以下命令启动Apache服务并设置服务开机自启动
systemctl start httpd
systemctl enable httpd
httpd默认监听端口为80,输入netstat -lntp查看是否启动成功
浏览器输入地址
说明安装成功
Apache开启.htaccess伪静态支持配置方法
- 检查Apache是否加载了mod_rewrite模块,找到httpd.conf文件,把一下行前面#去掉,没有则添加。
#LoadModule rewrite_module modules/mod_rewrite.so
- 检查Apache是否开启.htaccess支持
在httpd.conf中把 AllowOverride None 换成 AllowOverride All 使apache支持 .htaccess 文件。
#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
重启httpd即可,以下两种方式
systemctl restart httpd #重启
关闭和启动命令
systemctl start httpd #启动
systemctl stop httpd #停止服务