一.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的安装路径。
make
make install
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模式
make
make install
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
│ ├── p_w_picpaths
│ ├── 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
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start;;
stop)
stop;;
status)
status -p ${pidfile} $httpd
RETVAL=$?;;
restart)
stop
start;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi;;
reload)
reload;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?;;
*)
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))
显示效果如下图
加为系统服务并设置自动启动
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
make && make install
2.配置apache支持nagios
修改/etc/httpd/httpd.conf的文件
把 User deamon
Group deamon
改为
User nagios
Group nagios
然后找到
DirectoryIndex index.html
修改为
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脚本位置
# 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
Alias /nagios "/usr/local/nagios/share"#访问网页文件路径别名
# SSLRequireSSL
Options None
AllowOverride None
Require all granted
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
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里改配置文件
把
AllowOverride none
Require all denied
改为
AllowOverride none
Require all granted
并重启apache
nagios展示效果:
打开
http://172.30.65.169/nagios/的页面,输入nagios认证用户名nagiosadmin,和刚设置的密码
页面会显示如下图所示