http服务

1.编译安装httpd-2.4

httpd依赖于apr-1.4+,apr-util-1.4+,[apr-icon]

apr:apache portable runtime

安装开发环境
[root@ye ~]# yum groupinstall "Development Tools"

创建apache组与apache用户
[root@ye ~]# groupadd -r apache
[root@ye ~]# useradd -r -g apache apache
[root@ye ~]# [root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool

下载并安装apr-1.4+和apr-util-1.4+
[root@ye ~]# cd /usr/src/
[root@ye src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.5.tar.bz2
[root@ye src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
[root@ye src]# ls
apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  debug  kernels
[root@ye src]# tar xf apr-1.6.5.tar.bz2
[root@ye src]# tar xf apr-util-1.6.1.tar.bz2
[root@ye src]# ls
apr-1.6.5  apr-1.6.5.tar.bz2  apr-util-1.6.1  apr-util-1.6.1.tar.bz2  debug  kernels
[root@ye src]# cd apr-1.6.5
[root@ye apr-1.6.5]# vim configure
    cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"        //将此行加上注释,或者删除此行

[root@ye apr-1.6.5]# ./configure --prefix=/usr/local/apr    //执行配置文件
[root@ye apr-1.6.5]# make && make install    //安装

[root@ye apr-1.6.5]# cd /usr/src/apr-util-1.6.1
[root@ye apr-util-1.6.1]#  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr    //执行配置文件
[root@ye apr-util-1.6.1]# make && make install     //安装

编译安装httpd
[root@ye ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.bz2
[root@ye ~]# ls
anaconda-ks.cfg  httpd-2.4.37.tar.bz2
[root@ye ~]#  tar xf httpd-2.4.37.tar.bz2
anaconda-ks.cfg  httpd-2.4.37  httpd-2.4.37.tar.bz2
[root@ye ~]# cd httpd-2.4.37
[root@ye httpd-2.4.37]# ./configure --prefix=/usr/local/apache \
> --sysconfdir=/etc/httpd24 \
> --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-modules=most \
> --enable-mpms-shared=all \
> --with-mpm=prefork
[root@ye httpd-2.4.37]# make && make install
...
make[1]: Leaving directory `/root/httpd-2.4.37'

2.配置三种不同风格的虚拟主机

虚拟主机:

虚拟主机有三类:

  • 相同IP不同端口
  • 不同IP相同端口
  • 相同IP相同端口不同域名
设置主机名
[root@server30 ~]# vim /etc/httpd/conf/httpd.conf
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80       //取消此行前面的#号
......

在配置文件的最后加上如下内容
[root@server30 ~]# vim /etc/httpd/conf/httpd.conf
#virtual host 1     # 虚拟主机1的配置

    ServerName www.peng.com
    DocumentRoot "/var/www/html/peng"
    ErrorLog "/var/log/httpd/peng/error_log"
    CustomLog "/var/log/httpd/peng/access_log" combined
    
        
        Require all granted
        Require not ip 172.25.30.1
        
    


# virtual host 2     # 虚拟主机2的配置

    ServerName blog.ye.com
    DocumentRoot "/var/www/html/ye"
    ErrorLog "/var/log/httpd/ye/error_log"
    CustomLog "/var/log/httpd/ye/access_log" combined
    
        
          Require all granted
        
    


创建网页目录并修改属主属组
[root@server30 ~]# cd /var/www/html/
[root@server30 html]# mkdir www blog
[root@server30 html]# ls
ye  peng
[root@server30 html]# ll
total 0
drwxr-xr-x. 2 root root 6 Jan 18 00:35 ye
drwxr-xr-x. 2 root root 6 Jan 18 00:35 peng
[root@server30 html]# chown -R apache.apache ye
[root@server30 html]# chown -R apache.apache peng
[root@server30 html]# ll
total 0
drwxr-xr-x. 2 apache apache 6 Jan 18 00:35 ye
drwxr-xr-x. 2 apache apache 6 Jan 18 00:35 peng

创建网页
[root@server30 html]# pwd
/var/www/html
[root@server30 html]# ls
blog  www
[root@server30 html]# echo 'hello  peng' > peng/index.html
[root@server30 html]# echo 'hello ye' > ye/index.html

创建相应网页的日志目录
[root@server30 ~]# mkdir /var/log/httpd/{peng,ye}
[root@server30 ~]# ll /var/log/httpd/
total 0
drwxr-xr-x. 2 root root 6 Jan 18 00:48 ye
drwxr-xr-x. 2 root root 6 Jan 18 00:48 peng
[root@server30 ~]# chown -R apache.apache /var/log/httpd/

启动服务并查看是否有80端口
[root@server30 ~]#systemctl start httpd
[root@server30 ~]#ss -antl | grep 80
LISTEN     0      128         :::80                      :::*  

在客户机上验证
1.修改hosts文件
[root@server30 ~]#cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost
118.31.33.0 zabbix.forevercq.com
0.0.0.0 account.jetbrains.com
//添加以下2行
172.16.30.130 www.peng.com
172.16.30.130 www.ye.com

3.例子:

1.在server上配置一个web站点http://server.example.com;

需求:

  • 从http://ldap.example.com/pub/example.html下载文件,并重名为index.html,不要修改文件内容;
  • 将文件index.html拷贝到您的DocumentRoot目录下;
  • 来自于exampl.com的客户端可以访问该web服务器
安装http服务:
[root@server30 ~]# yum -y install httpd

进入/var/www/html/里面下载指定文件,并重命名:
[root@server30 ~]#  cd /var/www/html/
[root@server30 html]# wget http://ldap.example.com/pub/example.html
[root@server30 html]# ls
index.html
[root@server30 html]# mv example.html index.html

开启http服务,并设置开机自启:
[root@server30 html]# systemctl start httpd
[root@server30 html]# systemctl enable httpd.service

添加防火墙规则,并重新加载规则
[root@server30 html]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.30.0/24 service name=http accept' --permanent
success
[root@server30 html]#  firewall-cmd --reload
success

验证:
http服务_第1张图片

2.为站点http://server.example.com配置TLS加密;

需求:

  • 已签名证书从http://ldap.example.com/pub/server30.crt获取
  • 证书的秘钥从http://ldap.example.com/pub/server30.key获取
  • 证书的签名授权信息从http://ldap.example.com/pub/group30.crt获取
安装软件包mod_ssl
[root@server30 ~]# yum -y install mod_ssl

分别在指定目录下载指定文件
[root@server30 ~]# cd /etc/pki/tls/certs/
[root@server30 certs]# wget http://ldap.example.com/pub/server30.crt
[root@server30 certs]# wget http://ldap.example.com/pub/group30.crt
[root@server30 certs]# cd ..
[root@server30 tls]# cd private/
[root@server30 private]# wget http://ldap.example.com/pub/server30.key

编辑配置文件
vim /etc/httpd/conf.d/ssl.conf
#ServerName www.example.com:443    //修改前
ServerName server30.example.com:443    //修改后

然后将三个证书名及秘钥名进行修改
SSLCertificateFile /etc/pki/tls/certs/localhost.crt   //修改前
SSLCertificateFile /etc/pki/tls/certs/server30.crt   //修改后

SSLCertificateKeyFile /etc/pki/tls/private/localhost.key   //修改前
SSLCertificateKeyFile /etc/pki/tls/private/server30.key   //修改后

#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt   //修改前
SSLCACertificateFile /etc/pki/tls/certs/group30.crt    //修改后

重启服务
[root@server30 ~]# systemctl restart httpd

添加防火墙规则,并重新加载规则
[root@server30 ~]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.30.0/24 service name=https accept' --permanent
[root@server30 ~]# firewall-cmd --reload

验证:
http服务_第2张图片

3.在server上扩展您的WEB服务器

需求:

  • 为站点http://www.example.com创建一个虚拟主机
  • 设置DocumentRoot为/var/www/html
  • 从http://ldap.example.com/pub/www.html下载文件,并重名为index.html,不要修改文件内容
  • 将文件index.html拷贝到DocumentRoot目录下
  • 确保floyd用户能够在/var/www/virtual下创建文件
在/var/www下创建一个virtual/目录作为虚拟主机的DocumentRoot
[root@server30 ~]# cd /var/www
[root@server30 www]# mkdir virtual

将指定文件下载到/var/www/virtual目录下,并重命名
[root@server30 www]# wget -O virtual/index.html http://ldap.example.com/pub/www.html
[floyd@server30 www]$ ls virtual/
index.html

将/var/www目录下所有文件的属主属组都修改为apachet
[root@server30 www]# chown -R apache.apache /var/www/

创建用户floyd,并配置acl
[root@server30 www]# useradd floyd
[root@server30 www]# setfacl -m u:floyd:rwx virtual/

进入/etc/httpd/conf.d目录,全局下查找虚拟主机的配置文件,并复制到本地,进行编辑
[root@server30 www]# cd /etc/httpd/conf.d
[root@server30 conf.d]# find / -name *vhost*
[root@server30 conf.d]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf .
[root@server30 conf.d]# vim httpd-vhosts.conf
把虚拟主机信息修改为:

    DocumentRoot "/var/www/html"
    ServerName server30.example.com
    //新的虚拟主机


    DocumentRoot "/var/www/virtual"
    ServerName www.example.com
    //原来的虚拟主机

重启服务:
[root@server30 conf.d]# systemctl restart httpd.service 

验证:
在客户端上检测能否被解析:

[root@desktop30 ~]# ping www.example.com
PING www.example.com (172.16.30.130) 56(84) bytes of data.
64 bytes from alt.example.com (172.16.30.130): icmp_seq=1 ttl=64 time=0.466 ms
64 bytes from server30.example.com (172.16.30.130): icmp_seq=2 ttl=64 time=0.311 ms
64 bytes from alt.example.com (172.16.30.130): icmp_seq=3 ttl=64 time=0.250 ms
64 bytes from server30.example.com (172.16.30.130): icmp_seq=4 ttl=64 time=0.424 ms
^C
--- www.example.com ping statistics ---

在打开火狐浏览器验证:
http服务_第3张图片

返回服务端,检查floyd用户能够在/var/www/virtual下创建文件

[floyd@server30 ~]$ cd /var/www/virtual/
[floyd@server30 virtual]$ touch aa
[floyd@server30 virtual]$ ll 
total 4
-rw-rw-r--. 1 floyd  floyd   0 Jan 17 14:49 aa
-rw-r--r--. 1 apache apache 16 Nov 28  2014 index.html

4.Web访问控制

  • 在您的server上web服务器的DocumentRoot目录下创建一个名为private的目录,从http://ldap.example.com/pub/private.html下载文件到这个目录,并重命名为index.html,不要修改文件内容。
  • 从server上,任何人都可以浏览private的内容,但是从其他系统不能访问这个目录的内容
在/var/www/html下创建private目录,并将指定文件下载到此目录,并重名
[root@server30 ~]# cd /var/www/html/
[root@server30 html]# mkdir private
[root@server30 html]# wget -O private/index.html http://ldap.example.com/pub/private.html
[root@server30 html]# ls private/
index.html

编辑http的主配置文件
[root@server30 html]# cd
[root@server30 ~]# cd /etc/httpd/conf.d
[root@server30 conf.d]# vim httpd-vhosts.conf

在下面的ServerName server30.example.com的下面添加:

     Require ip 172.16.30.130
 

重启服务:
[root@server30 conf.d]# systemctl restart httpd

验证:能否在客户端上浏览

http服务_第4张图片

5.在server上实现动态web内容

  • 动态内容由名为alt.example.com的虚拟主机提供
  • 虚拟主机侦听端口为8909
  • 从http://ldap.example.com/pub/webapp.wsgi下载一个脚本,然后放在适当的位置,不要修改文件内容
  • 客户端访问http://alt.example.com:8909时,应该接受动态生成的web页面
  • 此http://alt.example.com:8909必须能被example.com内所有的系统访问
在/var/www目录下,创建一个新的目录wsgi,并下载指定文件,修改属主属组为apache
[root@server30 ~]# cd /var/www
[root@server30 www]# mkdir wsgi
[root@server30 www]# wget -O wsgi/webapp.wsgi http://ldap.example.com/pub/webapp.wsgi
[root@server30 www]# ls wsgi/
webapp.wsgi
[root@server30 www]# chown -R apache.apache wsgi/

编辑http的主配置文件
[root@server30 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
在最下面添加:
Listen 8909

    WSGIScriptAlias / "/var/www/wsgi/webapp.wsgi"
    ServerName alt.example.com


因为没有配置selinux,和wsgi相关的包没安装,所以服务起不来:
[root@server30 conf.d]# systemctl restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.

安装与wsgi相关包:
[root@server30 ~]# yum -y install mod_wsgi*

配置selinux:
[root@server30 ~]# semanage port -a -t http_port_t -p tcp 8909

现在就可以开启http服务了:
[root@server30 ~]# systemctl start httpd
[root@server30 ~]# ss -antl
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port 
LISTEN     0      100               127.0.0.1:25                       *:*     
LISTEN     0      128                       *:111                      *:*     
LISTEN     0      128                       *:41619                    *:*     
LISTEN     0      128                       *:22                       *:*     
LISTEN     0      128               127.0.0.1:631                      *:*     
LISTEN     0      100                     ::1:25                      :::*     
LISTEN     0      128                      :::52890                   :::*     
LISTEN     0      128                      :::443                     :::*     
LISTEN     0      128                      :::8909                    :::*     
LISTEN     0      128                      :::111                     :::*     
LISTEN     0      128                      :::80                      :::*     
LISTEN     0      128                      :::22                      :::*     
LISTEN     0      128                     ::1:631                     :::*     

添加防火墙规则:
[root@server30 ~]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.30.0/24 port port=8909 protocol=tcp accept' --permanent 
[root@server30 ~]# firewall-cmd --reload

验证:
在客户端上能否被解析:

[root@server30 ~]#  ping alt.example.com
PING alt.example.com (172.16.30.130) 56(84) bytes of data.
64 bytes from server30.example.com (172.16.30.130): icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from alt.example.com (172.16.30.130): icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from www.example.com (172.16.30.130): icmp_seq=3 ttl=64 time=0.055 ms
^C
--- alt.example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.055/0.065/0.082/0.015 ms

在火狐浏览器上验证:
http服务_第5张图片

你可能感兴趣的:(http服务)