当前位置: 首页 > 工具软件 > Simple HTTPD > 使用案例 >

基础服务——Httpd Web服务器

孟建木
2023-12-01

搭建Httpd Web服务器

 

[root@localhost ~]# rpm -qa | grep httpd                         检测系统是否安装httpd服务

[root@localhost ~]# tar zxf httpd-2.2.17.tar.gz -C /usr/src/           解压服务压缩包到指定目录

[root@localhost ~]# cd /usr/src/httpd-2.2.17/                 进入压缩目录下

[root@localhost httpd-2.2.17]# yum -y install gcc gcc-c++              安装gcc编译器

[root@localhost httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi             配置相应模块

 

上述配置命令具体参数含义:

--prefix         指定将httpd服务程序安装到那个目录下,如/usr/loca/httpd。

--enable-so启动动态加载模块支持,使httpd具备进一步扩展功能的能力。

--enable-rewrite 启动网页地址重写功能,用于网站优化及目录迁移维护。

--enable-charset-lite启动字符集支持,以便支持使用各种字符集编码的网页,如utf-8、gb231等 。

--enable-cgi启用cgi脚本程序支持,便于扩展网站的应用访问能力

 

[root@localhost httpd-2.2.17]# make         编译,将源代码编译成可执行程序

[root@localhost httpd-2.2.17]# make install   安装到预设目录下

[root@localhost httpd-2.2.17]# ls /usr/local/httpd/                   查看安装目录

bin    cgi-bin  error   icons    lib   man     modules

build  conf     htdocs  include  logs  manual

 

安装目录下,主要子目录的用途:

conf              存放httpd服务的各种配置文件,包括主配置文件httpd.conf、增强配置子目录extra等。

htdocs                   存放网页文档,包括默认首页文件indexhtml

logs               存放httpd服务的日志文件。

modules      存放httpd服务的各种模块文件。

cgi-bin          存放各种cgi程序文件。

 

[root@localhost httpd-2.2.17]# ln -s /usr/local/httpd/bin/* /usr/local/bin/                  创建软连接

[root@localhost httpd-2.2.17]# ls -l /usr/local/bin/httpd /usr/local/bin/apachectl    查看

lrwxrwxrwx. 1 root root 30 Apr 12 16:36 /usr/local/bin/apachectl -> /usr/local/httpd/bin/apachectl

lrwxrwxrwx. 1 root root 26 Apr 12 16:36 /usr/local/bin/httpd -> /usr/local/httpd/bin/httpd

[root@localhost httpd-2.2.17]# httpd –v   查看httpd版本信息

Server version: Apache/2.2.17 (Unix)

Server built:   Apr 12 2020 16:33:49

复制启动文件:

[root@localhost httpd-2.2.17]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd [root@localhost httpd-2.2.17]# vim /etc/init.d/httpd              编辑启动文件

#!/bin/sh

#

#chkconfig:35 25 25   35:运行等级,2525:开关机启动先后顺序

#description:haha    注释:冒号后面,可随便添加内容

 

[root@localhost httpd-2.2.17]# chkconfig --add httpd    httpd添加到系统服务中

[root@localhost httpd-2.2.17]# chkconfig --list httpd      查看httpd服务自启动状态

 

Note: This output shows SysV services only and does not include native

      systemd services. SysV configuration data might be overridden by native

      systemd configuration.

 

      If you want to list systemd services use 'systemctl list-unit-files'.

      To see services enabled on particular target use

      'systemctl list-dependencies [target]'.

 

httpd             0:off         1:off         2:off         3:on 4:off         5:on 6:off

[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf   添加域名

97 #ServerName www.example.com:80

 98 servername www.linux.com                这里可以使用快捷键98gg

 99

100 #

101 # DocumentRoot: The directory out of which you will serve your

 

[root@localhost httpd-2.2.17]# httpd –t             检测配置文件

Syntax OK

[root@localhost httpd-2.2.17]# vim /usr/local/httpd/htdocs/index.html     编辑网页文件

<html><body><h1>My name is ~</h1></body></html>

[root@localhost httpd-2.2.17]# systemctl start httpd     启动httpd服务

[root@localhost httpd-2.2.17]# netstat -anpt | grep httpd              查看服务启动状态

tcp6       0      0 :::80                   :::*                    LISTEN      65826/httpd       

[root@localhost httpd-2.2.17]# systemctl stop firewalld                   关闭防火墙

[root@localhost httpd-2.2.17]# setenforce 0             关闭沙盒

[root@localhost httpd-2.2.17]# firefox 192.168.0.20                  访问

 

日志分析系统:

 

[root@localhost ~]# tar zxf awstats-7.5.tar.gz -C /usr/src/                解压压缩包到指定目录

[root@localhost ~]# mv /usr/src/awstats-7.5/ /usr/local/awstats          更改路径和名字

[root@localhost ~]# cd /usr/local/awstats/tools/              进入配置目录

[root@localhost tools]# ls             查看

awstats_buildstaticpages.pl  dolibarr            maillogconvert.pl   xslt

awstats_configure.pl         geoip_generator.pl  nginx

awstats_exportlib.pl         httpd_conf          urlaliasbuilder.pl

awstats_updateall.pl         logresolvemerge.pl  webmin

                                                                                                             

awstats_configure.pl配置文件脚本

awstats_updateall.pl升级更新脚本

 

[root@localhost tools]# ./awstats_configure.pl      配置日志文件系统

 

----- AWStats awstats_configure 1.0 (build 20140126) (c) Laurent Destailleur -----

This tool will help you to configure AWStats to analyze statistics for

one web server. You can try to use it to let it do all that is possible

in AWStats setup, however following the step by step manual setup

documentation (docs/index.html) is often a better idea. Above all if:

- You are not an administrator user,

- You want to analyze downloaded log files without web server,

- You want to analyze mail or ftp log files instead of web log files,

- You need to analyze load balanced servers log files,

- You want to 'understand' all possible ways to use AWStats...

Read the AWStats documentation (docs/index.html).

 

-----> Running OS detected: Linux, BSD or Unix

 

-----> Check for web server install

 

Enter full config file path of your Web server.

Example: /etc/httpd/httpd.conf

Example: /usr/local/apache2/conf/httpd.conf

Example: c:\Program files\apache group\apache\conf\httpd.conf

Config file path ('none' to skip web server setup):

> /usr/local/httpd/conf/httpd.conf            输入httpd配置文件路径

 

-----> Check and complete web server config file '/usr/local/httpd/conf/httpd.conf'

Warning: You Apache config file contains directives to write 'common' log files

This means that some features can't work (os, browsers and keywords detection).

Do you want me to setup Apache to write 'combined' log files [y/N] ? y

  Add 'Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"'

  Add 'Alias /awstatscss "/usr/local/awstats/wwwroot/css/"'

  Add 'Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"'

  Add 'ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"'

  Add '<Directory>' directive

  AWStats directives added to Apache config file.

 

-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf'

  File awstats.model.conf updated.

 

-----> Need to create a new config file ?

Do you want me to build a new AWStats config/profile

file (required if first install) [y/N] ? y

 

-----> Define config file name to create

What is the name of your web site or profile analysis ?

Example: www.mysite.com

Example: demo

Your web site, virtual server or profile name:

> www.linux.com           输入域名

 

-----> Define config file path

In which directory do you plan to store your config file(s) ?

Default: /etc/awstats

Directory path to store config file(s) (Enter for default):          一直回车即可

>

 

-----> Create config file '/etc/awstats/awstats.www.linux.com.conf'

 Config file /etc/awstats/awstats.www.linux.com.conf created.

 

-----> Restart Web server with '/sbin/service httpd restart'

 

-----> Add update process inside a scheduler

Sorry, configure.pl does not support automatic add to cron yet.

You can do it manually by adding the following command to your cron:

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linux.com

Or if you have several config files and prefer having only one command:

/usr/local/awstats/tools/awstats_updateall.pl now

Press ENTER to continue...

 

 

A SIMPLE config file has been created: /etc/awstats/awstats.www.linux.com.conf

You should have a look inside to check and change manually main parameters.

You can then manually update your statistics for 'www.linux.com' with command:

> perl awstats.pl -update -config=www.linux.com

You can also read your statistics for 'www.linux.com' with URL:

> http://localhost/awstats/awstats.pl?config=www.linux.com

 

Press ENTER to finish...

 

[root@localhost tools]# vim /etc/hosts               编辑本地hosts文件

192.168.0.20 www.linux.com

 

编辑站点统计配置文件:

[root@localhost tools]# vim /etc/awstats/awstats.www.linux.com.conf    

  50 LogFile="/usr/local/httpd/logs/access_log"   指定日志路径,50            

220 DirData="/var/lib/awstats"                                指定数据目录,可以用为默认,但需要创建指定的目录,220

 

[root@localhost tools]# mkdir /var/lib/awstats                  创建数据目录

[root@localhost tools]# ./awstats_updateall.pl now                 执行升级更新脚本

Running '"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update -config=www.linux.com -configdir="/etc/awstats"' to update config www.linux.com

Create/Update database for config "/etc/awstats/awstats.www.linux.com.conf" by AWStats version 7.5 (build 20160301)

From data in log file "/usr/local/httpd/logs/access_log"...

Phase 1 : First bypass old records, searching new record...

Searching new records from beginning of log file...

Jumped lines in file: 0

Parsed lines in file: 3

 Found 0 dropped records,

 Found 0 comments,

 Found 0 blank records,

 Found 3 corrupted records,

 Found 0 old records,

 Found 0 new qualified records.

 

[root@localhost tools]# crontab –e             编辑计划任务

*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now

[root@localhost tools]# systemctl enable crond                开启计划任务

[root@localhost tools]# systemctl start crond

[root@localhost tools]# systemctl status crond                           查看状态

crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: active (running) since Sun 2020-04-05 06:36:47 CST; 1 weeks 0 days ago

 Main PID: 1089 (crond)

   CGroup: /system.slice/crond.service

           └─1089 /usr/sbin/crond -n

 

Apr 05 06:36:47 localhost.localdomain systemd[1]: Started Command Scheduler.

Apr 05 06:36:47 localhost.localdomain systemd[1]: Starting Command Scheduler...

Apr 05 06:36:48 localhost.localdomain crond[1089]: (CRON) INFO (RANDOM_DELAY ...

Apr 05 06:36:55 localhost.localdomain crond[1089]: (CRON) INFO (running with ...

Hint: Some lines were ellipsized, use -l to show in full.

 

[root@localhost tools]# chkconfig --level 35 crond on   将开启配置计划任务应用到35运行等级中

[root@localhost tools]# chkconfig --list crond         查看计划任务开启情况,以应用到35运行等级中

 

 

[root@localhost tools]# firefox http:www.linux.com/awstats/awstats.pl?config=www.linux.com           访问

 

 

[root@localhost tools]# vim /usr/local/httpd/htdocs/awb.html             简化域名(重写域名)

<html>

<head>

<meta http-equiv=refresh content="0;

url=http://www.linux.com/awstats/awstats.pl?config=www.linux.com">

</head>

<body></body>

</html>

[root@localhost tools]# systemctl restart httpd                 重启httpd服务

[root@localhost tools]# firefox www.linux.com/awb.com               访问

 

 

 

HTTPD虚拟主机的搭建

 

[root@localhost httpd-2.2.17]# cd /media/dvd/Packages/              进入rpm库目录

[root@localhost Packages]# rpm -ivh bind-9.9.4-37.el7.x86_64.rpm             安装DNS

warning: bind-9.9.4-37.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:bind-32:9.9.4-37.el7             ################################# [100%]

[root@localhost Packages]# setenforce 0          关闭沙盒

[root@localhost Packages]# vim /etc/named.conf           配置DNS

12 options {

 13         listen-on port 53 { any; };

 14         listen-on-v6 port 53 { ::1; };

 15         directory       "/var/named";

 16         dump-file       "/var/named/data/cache_dump.db";

 17         statistics-file "/var/named/data/named_stats.txt";

 18         memstatistics-file "/var/named/data/named_mem_stats.txt";

 19         allow-query     { any; };

 

[root@localhost Packages]# vim /etc/named.rfc1912.zones 配置DNS区域配置文件

zone "abc.com" IN {

        type master;

        file "abc.com.zone";

        allow-update { none; };

};

 

zone "123.com" IN {

        type master;

        file "123.com.zone";

        allow-update { none; };

};

 

注意:此处都为正向解析配置文件。

 

[root@localhost Packages]# cd /var/named/            进入DNS安装目录

[root@localhost named]# cp -p named.localhost abc.com.zone   复制区域配置文件

[root@localhost named]# cp -p named.localhost 123.com.zone

[root@localhost named]# vim abc.com.zone   配置abc域名的区域配置文件

$TTL 1D

@       IN SOA  @ abc.com. (

                                        0       ; serial

                                        1D      ; refresh

                                        1H      ; retry

                                        1W      ; expire

                                        3H )    ; minimum

        IN      NS      www.abc.com.

www     IN      A       192.168.0.20

dnssvr  IN      A       192.168.0.20

        AAAA    ::1

 

[root@localhost named]# vim 123.com.zone             配置123域名的区域配置文件

$TTL 1D

@       IN SOA  @ 123.com. (

                                        0       ; serial

                                        1D      ; refresh

                                        1H      ; retry

                                        1W      ; expire

                                        3H )    ; minimum

        IN      NS      www.123.com.

www     IN      A       192.168.0.20

dnssvr  IN      A       192.168.0.20

        AAAA    ::1

 

[root@localhost named]# vim /usr/local/httpd/conf/httpd.conf  配置httpd主配置文件

406 <IfModule ssl_module>

407 SSLRandomSeed startup builtin

408 SSLRandomSeed connect builtin

409 </IfModule>

410 include /usr/local/httpd/conf/extra/httpd-vhosts.conf

 

创建网页目录配置网页文件:

[root@localhost named]# mkdir /abc

[root@localhost named]# mkdir /123

[root@localhost named]# vim /abc/index.html

<h1>My name is baimawangzi</h1>

[root@localhost named]# vim /123/index.html

<h1>My name is xiaogongzhu</h1>

 

创建独立的配置文件:

[root@localhost named]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf

19 NameVirtualHost 192.168.0.20:80           设置虚拟主机监听地址

 20 <Directory "/abc">                           设置网页目录

 21     Order allow,deny

 22     allow from all

 23 </Directory>

 24 <Directory "/123">

 25     Order allow,deny

 26     allow from all

 27 </Directory>

 

34 <VirtualHost 192.168.0.20:80>         设置abc虚拟站点区域

 35     ServerAdmin webmaster@dummy-host.example.com //删除此行

 36     DocumentRoot "/abc" 

 37     ServerName www.abc.com

 38     ServerAlias www.dummy-host.example.com            //删除此行

 39     ErrorLog "logs/abc.com-error_log"

 40     CustomLog "logs/abc.com-access_log" common

 41 </VirtualHost>

 

42 <VirtualHost 192.168.0.20:80>         设置123虚拟站点区域

 43     DocumentRoot "/123"

 44     ServerName www.123.com

 45     ErrorLog "logs/123.com-error_log"

 46     CustomLog "logs/123.com-access_log" common

 46 </VirtualHost>

 

[root@localhost named]# systemctl restart httpd            重启httpd服务

[root@localhost named]# systemctl restart named                   重启DNS域名解析

[root@localhost named]# curl www.abc.com       访问

<h1>My name is baimawangzi</h1>

[root@localhost named]# curl www.123.com

<h1>My name is xiaogongzhu</h1>

 

 

基于IP的虚拟主机:

[root@localhost ~]# cd /etc/sysconfig/network-scripts/                                     进入网卡配置目录

[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0              复制一份

[root@localhost network-scripts]# vim ifcfg-ens33:0       配置

#DEFROUTE=yes

NAME=ens33:0

UUID=625b3a83-3d94-4858-ac71-f5efffc0a911                  //删除

DEVICE=ens33:0

IPADDR=192.168.0.20

NETMASK=255.255.255.0

GATEWAY=192.168.0.1

[root@localhost network-scripts]# systemctl restart network                 重启网卡

[root@localhost network-scripts]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:63:0a:5b brd ff:ff:ff:ff:ff:ff

    inet 192.168.0.10/24 brd 192.168.0.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.0.20/24 brd 192.168.0.255 scope global secondary ens33:0

       valid_lft forever preferred_lft forever

    inet6 fe80::fad2:529d:e831:6eb/64 scope link

       valid_lft forever preferred_lft forever

3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000

    link/ether 52:54:00:28:16:9f brd ff:ff:ff:ff:ff:ff

    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0

       valid_lft forever preferred_lft forever

4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000

link/ether 52:54:00:28:16:9f brd ff:ff:ff:ff:ff:ff

 

[root@localhost network-scripts]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 配置

34 <VirtualHost 192.168.0.20:80>

 35     DocumentRoot "/abc"

 36     ServerName www.abc.com

 37     ErrorLog "logs/abc.com-error_log"

 38     CustomLog "logs/abc.com-access_log" common

 39 </VirtualHost>

[root@localhost network-scripts]# systemctl restart httpd             重启httpd

[root@localhost network-scripts]# curl 192.168.0.20

<h1>This is bai ma wang zi</h1>

[root@localhost network-scripts]# curl 192.168.0.10

<h1>This is bai xue gong zhu</h1>

 

基于端口的虚拟主机:

 

删除刚刚创建的网卡:

[root@localhost network-scripts]# rm -fr /etc/sysconfig/network-scripts/ifcfg-ens33:0

[root@localhost network-scripts]# systemctl restart network                 重启网卡

[root@localhost network-scripts]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 配置

34 <VirtualHost 192.168.0.10:8080>

 35     DocumentRoot "/abc"

 36     ServerName www.abc.com

 37     ErrorLog "logs/abc.com-error_log"

 38     CustomLog "logs/abc.com-access_log" common

 39 </VirtualHost>

 40

 41 <VirtualHost 192.168.0.10:80>

 42     DocumentRoot "/123"

 43     ServerName www.123.com

 44     ErrorLog "logs/123.com-error_log"

 45     CustomLog "logs/123.com-access_log" common

 46 </VirtualHost>

[root@localhost network-scripts]# vim /usr/local/httpd/conf/httpd.conf   配置httpd

40 Listen 192.168.0.10:80

41 Listen 192.168.0.10:8080

[root@localhost network-scripts]# systemctl restart httpd             重启httpd

[root@localhost network-scripts]# curl 192.168.0.10:80

<h1>This is bai xue gong zhu</h1>

[root@localhost network-scripts]# curl 192.168.0.10:8080

<h1>This is bai ma wang zi</h1>

 类似资料: