Apache HTTP Server安装


title: Apache HTTP Server安装
categories: Linux
tags:
- Apache
- httpd
timezone: Asia/Shanghai
date: 2019-01-12


环境

CentOS6/7
REHL6/7

# 源码方式安装
[root@host66 bin]# ./httpd -v
Server version: Apache/2.4.37 (Unix)
Server built:   Jan  8 2019 09:15:52

# CentOS7 1804(7.5) YUM方式安装(安装光盘)
[root@localhost /]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09

# RHEL6.10 YUM方式安装(安装光盘)
[root@redhat6 ~]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Feb 19 2018 06:33:11

# REHL 7.3 YUM方式安装(安装光盘)
[root@localhost ~]# httpd -v
Server version: Apache/2.4.6 (Red Hat Enterprise Linux)
Server built:   Aug  3 2016 08:33:27

方法1:源码编译安装

1.配置本地yum并安装开发工具
mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom

cat <<EOF >/etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
EOF

yum clean all       #清理本地缓存
yum clean plugins   #清理插件缓存
yum makecache       #构建缓存

# 区分REHL6还是REHL7
which systemctl && yum group install -y "Development Tools" || yum groupinstall -y "Development Tools"
2.源码方式安装依赖项和Apache HTTP Server
# 安装expat-devel(这里注意使用yum安装,rpm安装后报错没有找到原因)
yum install expat-2.0.1-13.el6_8.x86_64.rpm expat-devel-2.0.1-13.el6_8.x86_64.rpm

# 下载依赖包
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
wget http://mirror.bit.edu.cn/apache//apr/apr-1.6.5.tar.gz
wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.gz
wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

# 安装pcre
tar vxzf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make
make install

# 这里为了保证每一步都正确安装使用echo $?命令查看命令执行结果是否有问题
# 0代表每问题,非0都是有问题的
tar -vxf apr-1.6.5.tar.gz
cd apr-1.6.5
./configure --prefix=/usr/local/apr
echo $?
make
echo $?
make install
echo $?


tar -vxf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
echo $?
make && make install && echo $?


tar -vxf httpd-2.4.37.tar.gz
cp -a apr-1.6.5 httpd-2.4.37/srclib/apr
cp -a apr-util-1.6.1 httpd-2.4.37/srclib/apr-util/
cd httpd-2.4.37
./configure --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
echo $?
make && make install && echo $?
3.启动httpd
默认安装路径:/usr/local/apache2/

# 编辑配置文件增加以下行
vim /usr/local/apache2/conf/httpd.conf
ServerName 0.0.0.0:80

# 启动httpd
/usr/local/apache2/bin/apachectl -k start
    
    -k start        启动
    -k restart      重新启动
    -k graceful     优雅的重启(重读配置文件,如果配置文件有问题,将继续用原来配置文件运行)
    -k graceful-stop    优雅的停止
    -k stop         停止

# 参数解析
[root@host66 bin]# /usr/local/apache2/bin/apachectl -h
Usage: /usr/local/apache2/bin/httpd [-D name] [-d directory] [-f file]
                                    [-C "directive"] [-c "directive"]
                                    [-k start|restart|graceful|graceful-stop|stop]
                                    [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in <IfDefine name> directives
  -d directory       : specify an alternate initial ServerRoot
                     # 
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
                     # 显示版本信息
  -V                 : show compile settings
                     # 显示编译设置
  -h                 : list available command line options (this page)
                     # 显示帮助信息
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed vhost settings
                     # 显示虚拟主机设置
  -t -D DUMP_RUN_CFG : show parsed run settings
                     # 显示已解析的运行设置
  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
                     # 等于-t -D DUMP_VHOSTS -D DUMP_RUN_CFG
  -t -D DUMP_MODULES : show all loaded modules 
                     # 显示所有加载模块
  -M                 : a synonym for -t -D DUMP_MODULES
                     # 等同于 -t -D DUMP_MODULES
  -t -D DUMP_INCLUDES: show all included configuration files
                     # 显示所有包含的配置文件
  -t                 : run syntax check for config files
                     # 检查配置文件
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)
                     # debug模式(只启动一个进程)

方法2:YUM方式安装

1.CentOS7(配置文件路径:/etc/httpd/conf/httpd.conf)

yum install -y httpd

systemctl start httpd
systemctl enable httpd
systemctl status httpd

2.REHL6(配置文件路径:/etc/httpd/conf/httpd.conf)

mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom

cat <<EOF >/etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
EOF

yum clean all       #清理本地缓存
yum clean plugins   #清理插件缓存
yum makecache       #构建缓存

yum install -y httpd

service httpd start
service httpd status
chkconfig httpd on
chkconfig --list httpd

3.REHL7(配置文件路径:/etc/httpd/conf/httpd.conf)

mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom

cat <<EOF >/etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
EOF

yum clean all       #清理本地缓存
yum clean plugins   #清理插件缓存
yum makecache       #构建缓存

yum install -y httpd

systemctl start httpd
systemctl enable httpd
systemctl status httpd

附录:配置文件常用参数介绍

# 监听端口
Listen 80

# 监听端口(多IP可以这么写)
Listen 10.0.1.66:8080
Listen 10.0.1.5:80

# 监听服务器名字(上边的端口号优先级更高)
ServerName 0.0.0.0:80

# 默认主页(如果默认主页不存在,默认将以目录模式展示文件列表)
<IfModule dir_module>
    DirectoryIndex 1.html
</IfModule>

# 禁用目录模式(去掉以下Indexes)
Options Indexes FollowSymLinks

附录:建立虚拟目录

# 编辑配置文件增加以下行
# 访问http://10.0.1.66/test的时候访问的是/home目录
vim /usr/local/apache2/conf/httpd.conf

Alias /test /home
<Directory "/home">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

附录:建立虚拟主机

1.基于端口的虚拟主机

Listen 8080
Listen 8090
# 访问:http://10.0.1.66:8080访问到的是/usr/local/apache2/111目录
<VirtualHost *:8080>
    ServerAdmin 111.com
    DocumentRoot "/usr/local/apache2/111"
    ErrorLog "logs/port80-error_log"
    CustomLog "logs/port80-access_log" common
</VirtualHost>
<Directory "/usr/local/apache2/111">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
# 访问:http://10.0.1.66:9090访问到的是/usr/local/apache2/222目录
<VirtualHost *:8090>
    ServerAdmin 222.com
    DocumentRoot "/usr/local/apache2/222"
    ErrorLog "logs/port8080-error_log"
    CustomLog "logs/port8080-access_log" common
</VirtualHost>
<Directory "/usr/local/apache2/222">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

2.基于IP地址的虚拟主机

Listen 80

# 访问:http://10.0.1.66访问到的是/usr/local/apache2/111目录
<VirtualHost 10.0.1.66:80>
    ServerAdmin 111.com
    DocumentRoot "/usr/local/apache2/111"
    ErrorLog "logs/port80-error_log"
    CustomLog "logs/port80-access_log" common
</VirtualHost>
<Directory "/usr/local/apache2/111">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
# 访问:http://10.0.1.5访问到的是/usr/local/apache2/222目录
<VirtualHost 10.0.1.5:80>
    ServerAdmin 222.com
    DocumentRoot "/usr/local/apache2/222"
    ErrorLog "logs/port8080-error_log"
    CustomLog "logs/port8080-access_log" common
</VirtualHost>
<Directory "/usr/local/apache2/222">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

3.基于域名的虚拟主机

Listen 80
# 访问111.com访问到的是/usr/local/apache2/111目录
<VirtualHost *:80>
    ServerAdmin 111.com
    ServerName 111.com
    DocumentRoot "/usr/local/apache2/111"
    ErrorLog "logs/port80-error_log"
    CustomLog "logs/port80-access_log" common
</VirtualHost>
<Directory "/usr/local/apache2/111">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
# 访问222.com访问到的是/usr/local/apache2/222目录
<VirtualHost *:80>
    ServerAdmin 222.com
    ServerName 222.com
    DocumentRoot "/usr/local/apache2/222"
    ErrorLog "logs/port8080-error_log"
    CustomLog "logs/port8080-access_log" common
</VirtualHost>
<Directory "/usr/local/apache2/222">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

附录:

Apache官网:http://httpd.apache.org/download.cgi

pcre-8.42.tar.gz下载地址:https://pan.baidu.com/s/1a8BjWf7FW7pER0dgoh_nwg

httpd-2.4.37.tar.gz下载地址:https://pan.baidu.com/s/17S2L9m79j6RWGr1x6CoGbw

apr-util-1.6.1.tar.gz下载地址:https://pan.baidu.com/s/1IhYxCU9uuDtHdKSzg-P_kg

apr-1.6.5.tar.gz下载地址:https://pan.baidu.com/s/1uNjTp2ob-Reurkyv4Cyubg

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,175评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,674评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,151评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,597评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,505评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,969评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,455评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,118评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,227评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,213评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,214评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,928评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,512评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,616评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,848评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,228评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,772评论 2 339

推荐阅读更多精彩内容