安裝之前準備:
配置好防火牆,selinux,時區.
檢查系統是否安裝了httpd服務
yum install gcc gcc-c++ zlib-devel
下載下傳軟體,pcre,apr,apr-util,apache
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
wget http://mirror.bit.edu.cn/apache//apr/apr-1.5.2.tar.gz
wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
建立軟體安裝目錄及下載下傳目錄
1.下載下傳目錄: /opt/dowanload
2.安裝目錄:
[root@bbs ~]# tree -L 1 /opt/app/
/opt/app/ #主目錄
├── apache -> /opt/app/apache2.4.25 #軟連接配接,友善以後更新及管理
├── apache2.4.25 #apache安裝目錄,所有檔案存放地
├── apr #apr 目錄
├── apr-util #apr-util 目錄
└── pcre #pcre 目錄
編譯安裝依賴包
tar zxf apr apr-util pcre
先安裝apr,apr-util依賴apr
#cd /opt/download
#cd apr-1.5.2
#./configure --prefix=/opt/app/apr
#make && make install
#cd apr-util-1.5.4
#./configure --prefix=/opt/app/apr-util --with-apr=/opt/app/apr
#make && make install
#cd pcre-8.40
#./configure --prefix=opt/app/pcre
開始編譯安裝apache
# cd http-2.4.25
# ./configure --prefix=/opt/app/apache2.4.25 --with-apr=/opt/app/apr --with-apr-util=/opt/app/apr-util --with-pcre=/opt/app/pcre --with-mpm=worker --enable-rewrite --enable-so --enable-headers --enable-expires --enable-deflate
# make && make install
虛拟主機篇(先備份好需要改動的配置檔案)
#軟連接配接 ln -s /opt/app/apache2.4.25 /opt/app/apache
預設安裝完成後有如下檔案:
[root@bbs extra]# tree -L 1 /opt/app/apache
/opt/app/apache
├── bin #啟動目錄
├── build
├── cgi-bin
├── conf
├── error
├── htdocs
├── icons
├── include
├── logs
├── man
├── manual
└── modules
[root@bbs ~]# tree -L 1 /opt/app/apache/conf/
/opt/app/apache/conf/
├── extra #此檔案存放虛拟主機配置檔案等
├── httpd.conf #主配置檔案
├── magic
├── mime.types
└── original
配置虛拟主機
[root@bbs extra]# tree -L 1 /opt/app/apache/conf/extra
/opt/app/apache/conf/extra
├── httpd-autoindex.conf
├── httpd-dav.conf
├── httpd-default.conf
├── httpd-info.conf
├── httpd-languages.conf
├── httpd-manual.conf
├── httpd-mpm.conf
├── httpd-multilang-errordoc.conf
├── httpd-ssl.conf
├── httpd-userdir.conf
├── httpd-vhosts.conf #虛拟主機檔案,
├── httpd-vhosts.conf.2017-04-05
└── proxy-html.conf
編輯虛拟主機文檔,跟2.4版本前的稍微有些變動
[root@bbs extra]# egrep -v "^$|#" /opt/app/apache/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin sadoc.blog.51cto.com
DocumentRoot "/var/www" #web網站存放目錄,請mkdir好目錄
ServerName bbs.52cto.com #域名
ServerAlias 52ctt.com #域名别名
ErrorLog "logs/bbs-error_log" #錯誤日志
CustomLog "logs/bbs-access_log" common #用戶端通路日志
</VirtualHost>
修改主配置檔案
DocumentRoot "/opt/app/apache2.4.25/htdocs"
<Directory "/opt/app/apache2.4.25/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#新增如下,不增加會報錯403錯誤.
<Directory "/var/www">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
#取消了vhost的加載注釋
Include conf/extra/httpd-vhosts.conf
apache服務啟動方式:
[root@bbs ~]# /opt/app/apache/bin/apachectl -t #檢查配置檔案是否正常.
Syntax OK
[root@bbs ~]# /opt/app/apache/bin/apachectl -k start #啟動服務
自己手動寫啟動腳本,友善啟動.
腳本寫的很簡單,也可以用程序數量判斷
[root@bbs ~]# cat /etc/init.d/httpd
#!/bin/bash
#2017-04-06
#加載函數庫
. /etc/rc.d/init.d/functions
#定義變量本腳本以Pid檔案是否存在判斷啟動.
PID="/opt/app/apache/logs/httpd.pid"
STARTING="/opt/app/apache/bin/apachectl"
start(){
if [ ! -s "$PID" ];then
$STARTING -k start
action "Apache Start" /bin/true
fi
}
stop(){
if [ -s "$PID" ];then
$STARTING -k stop
action "Apache Stop:" /bin/true
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop;
$0 start;
esac
# chmod +x /etc/init.d/httpd
用了腳本後,直接yum方式啟動,centos7可以用systemctl管理啟動
[root@bbs ~]# systemctl status httpd.service
● httpd.service - (null)
Loaded: loaded (/etc/rc.d/init.d/httpd; bad; vendor preset: disabled)
Active: active (running) since Wed 2017-04-05 17:20:03 CST; 22h ago
Docs: man:systemd-sysv-generator(8)
Process: 3873 ExecStart=/etc/rc.d/init.d/httpd start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/httpd.service
├─3877 /opt/app/apache2.4.25/bin/httpd -k start
├─3879 /opt/app/apache2.4.25/bin/httpd -k start
├─3880 /opt/app/apache2.4.25/bin/httpd -k start
├─3881 /opt/app/apache2.4.25/bin/httpd -k start
└─3963 /opt/app/apache2.4.25/bin/httpd -k start
Apr 05 17:20:03 bbs.52cto.com systemd[1]: Starting (null)...
Apr 05 17:20:03 bbs.52cto.com httpd[3873]: Apache Start [ OK ]
Apr 05 17:20:03 bbs.52cto.com systemd[1]: Started (null).
# 檢視服務端口
[root@bbs ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 3877 root 4u IPv6 29554 0t0 TCP *:http (LISTEN)
httpd 3879 daemon 4u IPv6 29554 0t0 TCP *:http (LISTEN)
httpd 3880 daemon 4u IPv6 29554 0t0 TCP *:http (LISTEN)
httpd 3881 daemon 4u IPv6 29554 0t0 TCP *:http (LISTEN)
httpd 3963 daemon 4u IPv6 29554 0t0 TCP *:http (LISTEN)
[root@bbs ~]# ps -ef | grep httpd
root 3877 1 0 Apr05 ? 00:00:03 /opt/app/apache2.4.25/bin/httpd -k start
daemon 3879 3877 0 Apr05 ? 00:00:00 /opt/app/apache2.4.25/bin/httpd -k start
daemon 3880 3877 0 Apr05 ? 00:00:00 /opt/app/apache2.4.25/bin/httpd -k start
daemon 3881 3877 0 Apr05 ? 00:00:00 /opt/app/apache2.4.25/bin/httpd -k start
daemon 3963 3877 0 Apr05 ? 00:00:00 /opt/app/apache2.4.25/bin/httpd -k start
root 6074 2380 0 15:45 pts/0 00:00:00 grep --color=auto httpd
查PID檔案
[root@bbs ~]# cat /opt/app/apache/logs/httpd.pid
3877
本文轉自 一招拜師 51CTO部落格,原文連結:http://blog.51cto.com/sadoc/1913461