本篇文章以两道题的形式道出:
一
:
建立httpd服务器(基于编译的方式进行),要求:
1)提供两个基于名称的虚拟主机:
(a)www1.magedu.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;
(b)www2.magedu.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;
(c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;
(d)通过www1.magedu.com/server-status输出httpd工作状态相关信息,且只允许提供帐号密码才能访问(status:status);
2)www1主机仅允许172.16.0.0/16网络中的客户机访问;www2主机可以被所有主机访问;
解决依赖关系:
- yum groupinstall -y "Development Libraries" "Development Tools" "X Software Development"
编译安装httpd服务器:
- lftp 172.16.0.1/pub/Sources/new_lamp 只限此实验环境内,可去官网下载哦
- mget apr-1.4.6.tar.bz2 apr-util-1.4.1.tar.bz2
(1) 编译安装apr
- # tar xf apr-1.4.6.tar.bz2
- # cd apr-1.4.6
- # ./configure --prefix=/usr/local/apr
- # make && make install
(2) 编译安装apr-util
- # tar xf apr-util-1.4.1.tar.bz2
- # cd apr-util-1.4.1
- #./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
- # make && make install
(3)httpd编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件包系统光盘自带:
- #yum install pcre-devel -y
为防止以下由于openssl版本过老而导致编译报错,安装openssl-devel解决问题:
- #yum install openssl-devel -y
2.编译httpd
- #tar xf httpd-2.4.3.tar.bz2
- #cd httpd-2.4.3
- # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-mpm:s-shared=all --with-mpm=event
- # make && make install
3、修改httpd的主配置文件,设置其Pid文件的路径
- vim /etc/httpd/httpd.conf,添加如下行即可:
- PidFile "/var/run/httpd.pid"
4、提供SysV服务脚本/etc/rc.d/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/conf/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/apache/bin/apachectl
- httpd=${HTTPD-/usr/local/apache/bin/httpd}
- prog=httpd
- pidfile=${PIDFILE-/var/run/httpd.pid}
- lockfile=${LOCKFILE-/var/lock/subsys/httpd}
- RETVAL=0
- start() {
- echo -n "Startingprog: "
- LANG=$HTTPD_LANG daemon --pidfile=pidfilehttpd $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && touch ${lockfile}
- return $RETVAL
- }
- stop() {
- echo -n "Stoppingprog: "
- killproc -p pidfile−d10httpd
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && rm -f lockfile{pidfile}
- }
- reload() {
- echo -n "Reloadingprog: "
- if ! LANG=HTTPDLANGhttpd $OPTIONS -t >&/dev/null; then
- RETVAL=$?
- echo $"not reloading due to configuration syntax error"
- failure "notreloadinghttpd due to configuration syntax error"
- else
- killproc -p pidfilehttpd -HUP
- RETVAL=$?
- fi
- echo
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status -p pidfilehttpd
- 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
而后为此脚本赋予执行权限:
- # chmod +x /etc/rc.d/init.d/httpd
加入服务列表:
- # chkconfig --add httpd
5.配置httpd.conf文件
- # vim /etc/httpd/httpd.conf
- 注释下列信息(默认设置为所有拒绝访问,这里需要注释掉)
- #<Directory />
- # AllowOverride none
- # Require all denied
- #</Directory>
- 启用:
- Include /etc/httpd/extra/httpd-vhosts.conf
- 编辑:
- # vim /etc/httpd/extra/httpd-vhosts.conf
- 注释原来信息添加新信息
- <VirtualHost *:80>
- ServerAdmin admin.magedu.com
- DocumentRoot "/web/vhosts/www1"
- ServerName www1.magedu.com
- ErrorLog "/var/log/httpd/www1.err"
- CustomLog "/var/log/httpd/www1.access" common
- <Location /server-status> 此处为server-status输出的信息
- SetHandler server-status
- AllowOverride Authconfig
- AuthType Basic
- AuthName "Limit..."
- AuthUserFile "/web/users"
- Require valid-user
- </Location>
- <Directory /web/vhosts/www1>
- Order Deny,Allow
- Deny from all
- Allow from 172.16.0.0/16
- </Directory>
- </VirtualHost>
- <VirtualHost 172.16.8.1:80>
- ServerAdmin admin.magedu.com
- DocumentRoot "/web/vhosts/www2"
- ServerName www2.magedu.com
- ErrorLog "/var/log/httpd/www2.err"
- CustomLog "/var/log/httpd/www2.access" common
- <Directory "/var/www/html/www2">
- Allow from all
- </Directory>
- </VirtualHost>
6.新建日志文件夹及其主页面文件夹与文件
- # mkdir -pv /var/log/httpd
- #mkdir -pv /web/vhosts/www2
- #mkdir -pv /web/vhosts/www1
- #echo "www1.magedu.com" > /web/vhosts/www1/index.html
- #echo "www2.magedu.com" > /web/vhosts/www2/index.html
7.添加访问的用户
- 首先定义环境变量:
- #echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
- #. /etc/profile
- htpasswd -c -m /web/users status
- htpasswd : -c 第一次创建的时候使用,以后再用表示覆盖源文件
- 账号密码为status:status
二
:为上题中的第1个虚拟主机提供php+mysql的功能,要求:
(1)通过在原有主页中添加phpinfo()测试页表明启用php成功;
(2)将mysql的root用户密码设置为"123456"(引号中的内容);
(3)通过http://www1.magedu.com/pma提供本机mysql服务的web管理接口phpMyAdmin;
(4)本机上的mysql服务仅允许来自本地的请求通过;
1,首先为MySQL创建数据目录:
- # mkdir -p /mydata/data
- # groupadd -r mysql
- # useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
- # chown -R mysql:mysql /mydata/data
2,编译安装MySQL,查看更改配置文件
- # tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local/
- # cd /usr/local/
- # ln -sv mysql-5.5.28-linux2.6-i686/ /usr/local/mysql
- # cd mysql
- # chown -R mysql:mysql .
- # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
- # chown -R root .
- # cd /usr/local/mysql
- # cp support-files/my-large.cnf /etc/my.cnf
- 查看更改配置文件:
- # vim /etc/my.cnf
- 更改
- thread_concurrency = 2 //第39行
- datadir = /mydata/data //新加一行
为mysql提供sysv服务脚本:
- # cd /usr/local/mysql
- # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- # chmod +x /etc/rc.d/init.d/mysqld
添加到服务列表中:
- # chkconfig --add mysqld
- # chkconfig mysqld on
而后就可以启动服务测试使用了。
- # service mysqld start
输出mysql的man手册至man命令的查找路径:
- # vim /etc/man.config
- MANPATH /usr/local/mysql/man //新加一行
输出mysql的头文件至系统头文件路径:
- # ln -sv /usr/local/mysql/include /usr/include/mysql
输出mysql的库文件给系统库查找路径:
- # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
- # ldconfig
设置环境变量:
- # vim /etc/profile.d/mysql.sh 添加如下
- export PATH=$PATH:/usr/local/apache/bin:/usr/local/mysql/bin
- # . /etc/profile
- # service mysqld start 重新启动
- # mysql 确认mysql正常运行(quit退出)
将mysql的root用户密码设置为"123456"
- # mysqladmin -uroot -p password
- 123456
php的安装配置:
解决依赖关系:
下载lftp 172.16.0.1/pub/Sources/ngnix目录中的如下几个rpm包并安装之:
- libmcrypt-2.5.7-5.el5.centos.i386.rpm
- libmcrypt-devel-2.5.5-5.el5.centos.i386.rpm
- mhash-0.9.2-6.el5.centos.i386.rpm
- mhash-devel-0.9.2-6.el5.centos.i386.rpm
- mcrypt-2.6.8-1.el5.i386.rpm
最好使用升级的方式安装上面的rpm包,命令格式如下:
- # rpm -Uvh
另外,也可以根据需要安装libevent,系统一般会自带libevent,但版本有些低。因此可以升级安装之,它包含如下两个rpm包
- 下载路径:ftp://172.16.0.1/pub/Sources/memcached。
- libevent-2.0.17-2.i386.rpm
- libevent-devel-2.0.17-2.i386.rpm
安装这两个包时,它可能会依赖于其他包的,所以可以用yum -y install 命令来安装
编译安装:
- # tar xf php-5.4.8.tar.bz2
- # cd php-5.4.8
- # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
- # make
- # make install
- # cp php.ini-production /etc/php.ini
配置测试:
- # vim /etc/httpd/httpd.conf
- 添加如下二行
- AddType application/x-httpd-php .php //378行
- AddType application/x-httpd-php-source .phps
- 将DirectoryIndex index.html修改为:
- DirectoryIndex index.php index.html
- # cd /web/vhosts/www1
- # vim index.php
- <?php
- phpinfo();
- ?>
重新启动httpd服务器
然后访问测试一下就OK了。
phpMyAdmin的安装配置:
- lftp 172.16.0.1:/pub/Sources/new_lamp>
- phpMyAdmin-3.5.1-all-languages.tar.bz2
- tar xf /root/phpMyAdmin-3.5.1-all-languages.tar.bz2
- mv phpMyAdmin-3.5.1-all-languages/* ./
- # tar xf phpMyAdmin-3.5.1-all-languages.tar.bz2
- # mkdir /web/vhosts/www1/pma
- # mv phpMyAdmin-3.5.1-all-languages/* /web/vhosts/www1/pma
仅允许来自本地的请求通过:
- #mysql
- #create database mydb;
- #grant all on mydb.* to root@'localhost' identified by '123456';
- #flush privileges;
- # service mysqld restart
以上为实验过程步骤,没有附加效果图,如有疑问可留言,笔者将以最大的努力解决疑问。。。
本文转自 z永 51CTO博客,原文链接:http://blog.51cto.com/pangge/1063366