title: Apache HTTP Server安裝
categories: Linux
tags:
- Apache
- httpd
timezone: Asia/Shanghai
date: 2019-01-12
環(huán)境
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并安裝開發(fā)工具
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 #構(gòu)建緩存
# 區(qū)分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 $?命令查看命令執(zhí)行結(jié)果是否有問題
# 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 優(yōu)雅的重啟(重讀配置文件吕晌,如果配置文件有問題憔古,將繼續(xù)用原來配置文件運行)
-k graceful-stop 優(yōu)雅的停止
-k stop 停止
# 參數(shù)解析
[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
# 顯示編譯設(shè)置
-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
# 顯示虛擬主機設(shè)置
-t -D DUMP_RUN_CFG : show parsed run settings
# 顯示已解析的運行設(shè)置
-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 #構(gòu)建緩存
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 #構(gòu)建緩存
yum install -y httpd
systemctl start httpd
systemctl enable httpd
systemctl status httpd
附錄:配置文件常用參數(shù)介紹
# 監(jiān)聽端口
Listen 80
# 監(jiān)聽端口(多IP可以這么寫)
Listen 10.0.1.66:8080
Listen 10.0.1.5:80
# 監(jiān)聽服務(wù)器名字(上邊的端口號優(yōu)先級更高)
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官網(wǎng):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