3、建立httpd服务器,要求:
1)提供两个基于名称的虚拟主机:
(a)www1.linuxidc.com,页面文件目录为/var/www/html/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;
(b)www2.linuxidc.com,页面文件目录为/var/www/html/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;
(c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;
2)www1主机仅允许172.16.0.0/16网络中的客户机访问;www2主机可以被所有主机访问;
参考答案
# yum groupinstall -y "Development Libraries" "Development Tools" "X Software Development"
# yum install -y pcre pcre-devel
# rpm -Uvh apr-1.4.6-1.i386.rpm
# rpm -Uvh apr-devel-1.4.6-1.i386.rpm
# rpm -Uvh apr-util-1.4.1-1.i386.rpm
# rpm -Uvh apr-util-devel-1.4.1-1.i386.rpm
# yum install -y pcre pcre-devel
如果没有以上版本的apr和apr-util软件包的rpm版,也可以使用源码包编译安装
安装的时候指定--prefix路径,然后在编译apache的时候,使用--apr=/path/to/apr --apr-util=/path/to/apr-util
# tar xf httpd-2.4.1.tar.bz2
# cd httpd-2.4.1
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd/ --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib
# make
# make install
# vim /etc/httpd/httpd.conf
把第464行的#号去掉,结果如下
463 # Virtual hosts
464 Include /etc/httpd//extra/httpd-vhosts.conf
# cd /etc/httpd/extra
# vim httpd-vhosts.conf
将系统自带的虚拟主机给注释掉(第23到38行前面加#号),然后仿照系统自带的自己写两个虚拟主机,配置如下
<VirtualHost *:80>
ServerAdmin admin@linuxidc.com
DocumentRoot "/var/www/html/www1"
ServerName "www1.linuxidc.com"
CustomLog "/var/log/httpd/www1.access" common
ErrorLog "/var/log/httpd/www1.err"
<Directory "/var/www/html/www1">
Require ip 172.16.0.0/16
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@linuxidc.com
DocumentRoot "/var/www/html/www2"
ServerName "www2.linuxidc.com"
CustomLog "/var/log/httpd/www2.access" common
ErrorLog "/var/log/httpd/www2.err"
<Directory "/var/www/html/www2">
Require all granted
</Directory>
</VirtualHost>
# mkdir -p /var/www/html/www1
# mkdir -p /var/www/html/www2
# mkdir -p /var/log/httpd/
# cd /var/www/html/www1/
# echo "<h1><center>www1.linuxidc.com</center></h1>" >index.html
# cd ../www2
# echo "<h1><center>www2.linuxidc.com</center></h1>" >index.html
# /usr/local/apache/bin/apachectl start
4、建立nginx服务器,要求:
1)通过8080端口提供服务;
2)提供两个基于名称的虚拟主机(都通过8080端口提供服务):
(a)www1.linuxidc.com:8080,页面文件目录为/www/html/www1;错误日志为/var/log/nginx/www1.err,访问日志
为/var/log/nginx/www1.access;
(b)www2.linuxidc.com:8080,页面文件目录为/www/html/www2;错误日志为/var/log/nginx/www2.err,访问日志
为/var/log/nginx/www2.access;
3)www2主机仅允许172.16.0.0/16网络中的主机访问;
4)为nginx提供sysv服务控制脚本;
5)本机的8080端口仅允许已经建立的连接请求出站,仅允许新请求和已建立连接的请求入站;
参考答案:
# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx
# tar xf nginx-1.0.14.tar.gz
# cd nginx-1.0.14
#
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
# make
# make install
# vim /etc/rc.d/init.d/nginx
写入以下内容
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
# chmod +x /etc/rc.d/init.d/nginx
# chkconfig --add nginx
# vim /etc/nginx/nginx.conf 添加以下内容,把原来的server虚拟主机给注释掉,不然会跟apache的80端口冲突
以下内容,要写到http关键字里面,不能写到其它server或者location里面,不然会报错
server {
listen 8080;
server_name www1.linuxidc.com;
root /www/html/www1;
index index.html;
access_log /var/log/nginx/www1.access main;
error_log /var/log/nginx/www1.err;
}
server {
listen 8080;
server_name www2.linuxidc.com;
root /www/html/www2/;
index index.html;
access_log /var/log/nginx/www2.access;
error_log /var/log/nginx/www2.err;
location / {
allow 172.16.0.0/16;
deny all;
}
}
# mkdir -p /www/html/www1
# mkdir -p /www/html/www2
# cd /www/html/www1/
# echo "<h1><center>Nginx www1.linuxidc.com</center></h1>" >index.html
# cd ../www2
# echo "<h1><center>Nginx www2.linuxidc.com</center></h1>" >index.html
# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
# iptables -P INPUT DROP
# iptables -P OUTPUT DROP
# iptables -A INPUT -p tcp --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -p tcp --sport 8080 -m state --state ESTABLISHED -j ACCEPT
# iptables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
