centos7——源码安装Apache

下载需要的软件包

下载 APR 和 APR-util 官方链接
在这里插入图片描述在这里插入图片描述下载 pcre ,这里不是pcre2 官方链接
在这里插入图片描述
下载 httpd 官方地址
在这里插入图片描述将下载的这四个软件包上传至centos7
在这里插入图片描述

开始进行源码安装

1.配置安装需要的环境

yum install -y gcc gcc-c++ make expat-devel 

2.查看上传的文件
在这里插入图片描述3.解压安装
(一) 首先解压apr,并设施安装目录
在这里插入图片描述

tar zxf apr-1.7.0.tar.gz
cd apr-1.7.0/
./configure --prefix=/usr/local/apr 
make && make install

(二)解压apr-utils,设置安装目录,引用apr 解决依赖问题
在这里插入图片描述

tar zxf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1/ 
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install

(三)解压pcre,并安装
在这里插入图片描述

tar zxf pcre-8.44.tar.gz
cd pcre-8.44/ 
./configure --prefix=/usr/local/pcre  #设置安装路径
make && make install

(四)解压httpd,并安装

tar zxf httpd-2.4.43.tar.gz -C /usr/src/  #设置解压路径
cd /usr/src/httpd-2.4.43/ 
./configure --prefix=/usr/local/apache --sysconfdir=/etc --enable-so --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre
#配置安装路径,指定config文件,启动动态控制支持,启动地址重启,依赖APR,依赖APR-util,依赖pcre
make && make install

Apache启动相关配置

1、配置防火墙

firewall-cmd --permanent --add-service=http

或者将80端口配置到防火墙

firewall-cmd --permanent --add-port=80/tcp

重新启动防火墙

firewall-cmd --reload

关闭SElinux安全策略

setenforce 0

在配置文件中添加配置(详细见 本文最后的错误解释)

vim /etc/httpd.conf

2、开启或关闭 httpd服务
方法一、
因为安装目录在 /usr/local/apache下,因此命令为

/usr/local/apache/bin/apachectl start
/usr/local/apache/bin/apachectl stop

方法二、
设置连接

ln -s /usr/local/apache/bin/* /usr/sbin/

httpd检测

httpd -t

启动或关闭服务

httpd -k start / stop

3、设置开机自享

cp /usr/local/apache/bin/apache/bin/apcahectl /etc/init.d/httpd

编辑 /etc/init.d/httpd 文件,在第一行 #!/bin/bash 的后面添加如下两行
说明:2345 表示脚本运行的级别,即在2、3、4、5这4种模式下都可以运行,70 表示脚本启动的顺序号,40 表示系统关闭时,脚本的停止顺序号

chkconfig: 2345 70 40
description:apache
chkconfig --add httpd
chkconfig httd on
chkconfig --list httpd

设置完毕后也可以使用systemctl
启动 Apache 服务

systemctl start httpd.service

查看 Apache 服务运行状态

systemctl status httpd.service

关闭 Apache 服务

systemctl stop httpd.service

补充内容

配置文件的内容:/etc/httpd.conf 更改文件目录

DocumentRoot "/var/www/html" //放入项目得位置可以进行更改到项目中得public
ServerName www.hdc.edu.cn //域名
<Directory />
    Options FollowSymLinks //页面显示indexs
    AllowOverride None
</Directory>
 
<Directory "/var/www/html">
Options Indexes FollowSymLinks//页面显示indexs
AllowOverride None
    Order allow,deny
    Allow from all //允许所有访问 否则可能出现404错误
</Directory>
 
AccessFileName .htaccess
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</Files>

源码安装更改网站根目录更改此处
在这里插入图片描述
改为(如果/var/www 下没有html 文件夹,会报错,提前建好该文件)
在这里插入图片描述
更改端口只需要更改Listen 后的端口数,改完防火墙放行端口

注意每次改完httpd.conf配置后需要重启httpd服务,

防火墙添加端口后也需要重启防火墙服务

Apache中的虚拟主机配置参考以下信息

#Listen  80 原来设置
#Listen  81 新增设置内容,在另外一个端口监听
###=====找httpd_vhosts.con文件=====###
#<VirtualHost *:81> 重新填写该端口所对应的管理员,根目录主机名,日志信息等内容
#    ServerAdmin wbm@hdc.edu.cn
#    DocumentRoot /var/www/html/tp51worker
#    ServerName  www.worker.com  #修改host文件
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

更改配置完毕后记得重新启动服务

错误

AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName’ directive globally to suppress this message
解决:
因为在上面安装httpd的时候,将httpd.conf 配置文件放置到 /etc文件加下,源码在 /usr/src 目录下
安装目录在 /usr/local 文件夹,了解安装的目录后。
根据报错我们知道需要配置一个ServerName,因此需要改配置文件 httpd.conf(在/etc下) ,
如图:
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值