Centos7安装apache服务器

官网地址: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伪静态支持配置方法

  1. 检查Apache是否加载了mod_rewrite模块,找到httpd.conf文件,把一下行前面#去掉,没有则添加。
#LoadModule rewrite_module modules/mod_rewrite.so
  1. 检查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 #停止服务
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值