天天看點

apache 編譯安裝與做nagios前端展示

一.apache編譯安裝篇

1.安裝apache需安裝以下的幾個包,apr 、apr-util、pcre等。

2.下載下傳安裝apr

把檔案放到/usr/local/src目錄下,

tar -zxvf  apr-1.5.2.tar.gz

cd  apr-1.5.2

./configure --prefix=/usr/local/apr

make

make install

3.下載下傳安裝apr-util

tar -zxvf apr-util-1.5.4.tar.gz

cd apr-util-1.5.4

./configure --prefix=/usr/local/apr-util  --with-apr=/usr/local/apr#這裡配置的時候要指定apr的安裝路徑。

4.安裝pcre

tar -zxvf pcre

cd pcre

./configure --prefix=/usr/local/pcre

make && make install

5.安裝apache 這裡安裝的版本是2.4.18,比較新的版本

tar -zxvf httpd-2.4.18.tar.gz

cd  httpd-2.4.18

./configure --prefix=/usr/local/apache2 --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-cgid --enable-rewrite --enable-deflate --with-z  --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --with-openssl=/usr/local/ssl  --enable-modules=most --enable-mpms-shared=all --with-mpm=event

#各編譯參數說明

--prefix=/usr/local/apache2 # 家目錄

--sysconfdir=/etc/httpd #配置檔案目錄

--enable-so #加載動态共享對象,可實作子產品動态生效

--enable-ssl #支援SSL/TLS,可實作https通路

--enable-ssl #支援CGI腳本(預設對非線程的MPM模式開啟)

--enable-rewrite #啟用Rewirte功能

--enable-deflate #支援壓縮

--with-zlib#指定zlib庫,不指定自動尋找

--with-apr=/usr/local/apr #指定apr路徑

--with-apr-util=/usr/local/apr-util #指定apr-util路徑

--with-pcre=/usr/local/pcre #指定pcre路徑

--with-openssl=/usr/local/ssl  #指定openssl的路徑

--enable-modules=most#指定動态啟用的子產品

--enable-mpms-shared=all #支援動态加載的MPM子產品,可選"all"

--with-mpm=event#設定預設啟用的MPM模式

6.安裝完成後安裝目錄下會有以下的幾個目錄

bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules

[root@sever9 ~]# tree -d /usr/local/apache2#安裝目錄樹

/usr/local/apache2

├── bin#主程式目錄

├── build

├── cgi-bin#cgi檔案存放目錄

├── error#發生錯誤時傳回給用戶端的資訊

│   └── include

├── htdocs

├── icons#httpd圖示檔案

│   └── small

├── include#頭檔案

├── logs#日志檔案

├── man#幫助手冊

│   ├── man1

│   └── man8

├── manual

│   ├── developer

│   ├── faq

│   ├── howto

│   ├── images

│   ├── misc

│   ├── mod

│   ├── platform

│   ├── programs

│   ├── rewrite

│   ├── ssl

│   ├── style

│   │   ├── css

│   │   ├── lang

│   │   ├── latex

│   │   ├── scripts

│   │   └── xsl

│   │       └── util

│   └── vhosts

└── modules#子產品檔案

7.配置目錄下的檔案

[root@sever9 httpd]# tree /etc/httpd

/etc/httpd

├── 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

│   └── proxy-html.conf

├── httpd.conf#主配置檔案

├── magic

├── mime.types

└── original

    ├── 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

    │   └── proxy-html.conf

    └── httpd.conf

8.修改配置檔案

vim /etc/httpd/httpd.conf

找到下面的該行,把監聽端口改成本地的80

#ServerName www.example.com:80

ServerName  localhost:80

修改httpd的主配置檔案,設定Pid檔案的路徑

PidFile "/var/run/httpd.pid"

修改系統的PATH環境變量,讓/usr/local/apache2/bin目錄下的指令都可以執行:

vim /etc/profile.d/httpd.sh

export PATH=/usr/local/apache2/bin:$PATH

source /etc/profile.d/httpd.sh

檢查下文法

[root@sever9 ~]# httpd -t  

Syntax OK

導出頭檔案

ln -sv /usr/local/apache2/include /usr/local/include/httpd

`/usr/local/include/httpd' -> `/usr/local/apache2/include'

導出man手冊,可以用man httpd檢視http的指令

vim /etc/man.config

MANPATH /usr/local/apache2/man

9.編輯服務腳本

vim /etc/init.d/httpd

#!/bin/bash

#

# httpd  Startup script for the Apache HTTP Server

# chkconfig: - 85 15

# description: Apache is a World Wide Web server.  It is used to serve \

#  HTML files and CGI.

# processname: httpd

# config: /etc/httpd/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid

# Source function library.

. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then

    . /etc/sysconfig/httpd

fi

# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/apache2/bin/apachectl#apache 控制腳本路徑httpd=${HTTPD-/usr/local/apache2/bin/httpd}#apache 主程式的路徑

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}#注意該http.pid檔案的路徑lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0

start() {

  echo -n $"Starting $prog: "

  LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

  RETVAL=$?

  echo

  [ $RETVAL = 0 ] && touch ${lockfile}

  return $RETVAL

}

stop() {

   echo -n $"Stopping $prog: "

   killproc -p ${pidfile} -d 10 $httpd

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

reload() {

  echo -n $"Reloading $prog: "

  if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

    RETVAL=$?

    echo $"not reloading due to configuration syntax error"

    failure $"not reloading $httpd due to configuration syntax error"

  else

    killproc -p ${pidfile} $httpd -HUP

  fi

# See how we were called.

case "$1" in

start)

  start;;

stop)

  stop;;

status)

  status -p ${pidfile} $httpd

  RETVAL=$?;;

restart)

  stop

condrestart)

  if [ -f ${pidfile} ] ; then

    stop

    start

  fi;;

reload)

  reload;;

graceful|help|configtest|fullstatus)

  $apachectl $@

*)

  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

  exit 1

esac

exit $RETVAL

10.為腳本加執行權限,并啟動測試

chmod +x /etc/init.d/httpd

[root@sever9 ~]# chmod +x /etc/rc.d/init.d/httpd

[root@sever9 ~]# service httpd start

Starting httpd:                                            [  OK  ]

檢視監聽的端口

[root@sever9 ~]# ss -tnlp

LISTEN      0      128                                                                      :::80                                                                      :::*      users:(("httpd",16817,4),("httpd",16819,4),("httpd",16820,4),("httpd",16821,4))

顯示效果如下圖

<a href="http://s5.51cto.com/wyfs02/M01/7E/7E/wKiom1cCQNGAKYzBAABEgCFukzM915.png" target="_blank"></a>

加為系統服務并設定自動啟動

chkconfig --add httpd

chkconfig httpd on

二.做nagios前端展示篇

1.編譯安裝php

這裡我們安裝的版本是5.6.11,

tar -jxvf php-5.6.11.tar.bz2

cd php-5.6.11

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs

2.配置apache支援nagios

修改/etc/httpd/httpd.conf的檔案

把 User deamon

Group deamon

改為

User nagios

Group nagios

然後找到 

&lt;IfModule dir_module&gt;

DirectoryIndex index.html

&lt;/IfModule&gt;

修改為 

DirectoryIndex index.html index.php

AddType application/x-httpd-php .php

再找到子產品項,把下面的幾個子產品選項注釋去掉。

LoadModule cgid_module modules/mod_cgid.so

LoadModule actions_module modules/mod_actions.so

為了安全起見,一般情況下要讓nagios 的web監控頁面必須經過授權才能通路,這需要增加驗證配置,即在httpd.conf檔案最後添加如下資訊:

定義别名:

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"#nagiosCGI腳本位置

&lt;Directory "/usr/local/nagios/sbin"&gt;

#  SSLRequireSSL

    Options ExecCGI

    AllowOverride None

    Require all granted

#  Allow from 127.0.0.1

   AuthName "Nagios Access"

   AuthType Basic

   AuthUserFile /usr/local/nagios/etc/htpasswd.users#nagios使用者認證檔案

   Require valid-user

&lt;/Directory&gt;

Alias /nagios "/usr/local/nagios/share"#通路網頁檔案路徑别名

&lt;Directory "/usr/local/nagios/share"&gt;

   Options None

   AllowOverride None   

   Require all granted

#  Order deny,allow

#  Deny from all

   AuthUserFile /usr/local/nagios/etc/htpasswd.users

3.建立apache目錄驗證檔案 

在上面的配置中,指定了目錄驗證檔案htpasswd,下面要建立這個檔案:

/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

New password: 

Re-type new password: 

Adding password for user nagiosadmin

4.關閉并重新開機apache

service httpd restart

啟動apache,報403拒絕通路,這裡是2.4*的版本

要在httpd.conf裡改配置檔案

&lt;Directory /&gt;

    AllowOverride none

    Require all denied

    Require all granted

并重新開機apache

nagios展示效果:

打開

http://172.30.65.169/nagios/的頁面,輸入nagios認證使用者名nagiosadmin,和剛設定的密碼

頁面會顯示如下圖所示

<a href="http://s4.51cto.com/wyfs02/M01/7D/33/wKiom1biN7SQVCcQAAI1MtLLlsA402.png" target="_blank"></a>

本文轉自伺服器運維部落格51CTO部落格,原文連結http://blog.51cto.com/shamereedwine/1749865如需轉載請自行聯系原作者

neijiade10000