WWW Server

出自Centos

跳转到: 导航, 搜索

目录

[编辑] WWW(apache)介绍

对于大多数的网络服务器,最广泛的功能就是开通HTTP服务,达到开放和发布网站的目的。目前绝大多数的网络交互程序,例如论坛、社区等等也是基于HTTP而建立的。

  在这里,我们通过Web服务器软件中使用量超过半数的自由软件Apache,来构建一台能够满足实际要求的多功能Web服务器,使它能够在我们发布网页的基础上,实现可以运行Perl、PHP的等等语言编写的交互程序,以及在虚拟主机、保密方面的功能。

[编辑] 所需安装包

   * httpd  :Apache HTTP Server 官方网址: http://httpd.apache.org/
   * mysql  :mysql客户端程序及共享库 官方网址:http://www.mysql.com
   * mysql-server :mysql服务器及相关文件 官方网址:http://www.mysql.com
   * php :Hypertext Preprocessor 官方网址:http://www.php.net
   * php-devel (这个与 PHP 加速软体有关)
   * php-mysql 一个模块式用户使的php程序可使用mysql数据库

[编辑] 安装相应的组件

  yum -y install httpd mysql mysql-server php php-devel php-mysql

[编辑] 配置HTTP服务器

[编辑] 开启URL重写功能

<Directory "/var/www/html">
Options Indexes FollowSymLinks
RewriteEngine on  <--增加此语句
AllowOverride All <--把None修改为All
Order allow,deny
Allow from all
</Directory>

[编辑] 配置httpd支持wap功能

修改httpd.conf配置文件, 在 httpd.conf 文件中 AddType application/x-gzip .gz .tgz 行下面添加如下内容:

#Wap MIME
AddType text/vnd.wap.wml .Wml
AddType application/vnd.wap.wmlc .Wmlc
AddType image/vnd.wap.wbmp .Wbmp
AddType application/vnd.wap.wmlscriptc .wmlsc
AddType text/vnd.wap.wmlscript .Wmls
AddType application/vnd.wap/wmlscriptc .Wsc

在 DirectoryIndex index.html 后添加index.wml 然后使用apachectl restart重启httpd


[编辑] 配置同ip的并发数

模块官方地址:http://dominia.org/djao/limitipconn.html

另外也提供yum安装方式:http://atrpms.net

参考资料:http://www.37dg.com/tutorial/248.jsp

//下载Centos5二进制rpm包
[root@localhost ~]# wget http://dominia.org/djao/limit/mod_limitipconn-0.23-1.el5.i386.rpm
--09:01:25--  http://dominia.org/djao/limit/mod_limitipconn-0.23-1.el5.i386.rpm
Resolving dominia.org... 18.208.0.43
Connecting to dominia.org|18.208.0.43|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20083 (20K) [application/x-rpm]
Saving to: `mod_limitipconn-0.23-1.el5.i386.rpm'

100%[====================================================================================================================>] 20,083      61.0K/s   in 0.3s

09:01:26 (61.0 KB/s) - `mod_limitipconn-0.23-1.el5.i386.rpm' saved [20083/20083]
//安装刚下载的rpm包
[root@localhost ~]# rpm -Uvh mod_limitipconn-0.23-1.el5.i386.rpm
Preparing...                ########################################### [100%]
   1:mod_limitipconn        ########################################### [100%]
//查看一下此包包含哪些文件
[root@localhost ~]# rpm -ql mod_limitipconn
/etc/httpd/conf.d/limitipconn.conf
/usr/lib/httpd/modules/mod_limitipconn.so
/usr/share/doc/mod_limitipconn-0.23
/usr/share/doc/mod_limitipconn-0.23/ChangeLog
/usr/share/doc/mod_limitipconn-0.23/README
//编辑配置文件
[root@localhost ~]#vi /etc/httpd/conf.d/limitipconn.conf
LoadModule limitipconn_module modules/mod_limitipconn.so
ExtendedStatus On
<IfModule mod_limitipconn.c>
    # Add configuration directives here
    <Location />      #所限制的目录所在,此处表示主机的根目录
  MaxConnPerIP 3    #所限制的每个IP并发连接数为3个
  NoIPLimit image/* #对图片不做IP限制
  </Location>
  <Location /mp3>   #所限制的目录所在,此处表示主机的/mp3目录
  MaxConnPerIP 1    #所限制的每个IP并发连接数为1个
  OnlyIPLimit audio/mpeg video #该限制只对视频和音频格式的文件
  </Location> 
</IfModule>
//此配置文件可参考/usr/share/doc/mod_limitipconn-0.23/README文件配置

[编辑] 限制带宽

查阅了一下apache mod_bandwidth 官方网站:http://www.cohprog.com/mod_bandwidth.html 好像没有提供apache 2.0版本以后的模块,看到有人说测试不是很稳定:参考:http://hi.chinaunix.net/?31789/viewspace-7455

看来只能使用TC进行解决, 或者使用此模块 官方地址: http://bwmod.sourceforge.net/ http://ivn.cl/apache 参考文件: http://www.tzlink.com/bbs/thread-3510-1-1.html 经过测试,发现能支持apache2,配置如下:

//下载源代码
[root@localhost ~]# wget http://bwmod.sourceforge.net/files/mod_bw-0.7.tgz
--10:11:00--  http://bwmod.sourceforge.net/files/mod_bw-0.7.tgz
Resolving bwmod.sourceforge.net... 216.34.181.96
Connecting to bwmod.sourceforge.net|216.34.181.96|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 18739 (18K) [application/x-gzip]
Saving to: `mod_bw-0.7.tgz'

100%[====================================================================================================================>] 18,739      18.2K/s   in 1.0s

10:11:03 (18.2 KB/s) - `mod_bw-0.7.tgz' saved [18739/18739]

[root@localhost ~]# tar zxvf mod_bw-0.7.tgz
mod_bw/
mod_bw/ChangeLog
mod_bw/TODO
mod_bw/mod_bw.c
mod_bw/LICENSE
mod_bw/mod_bw.txt
[root@localhost ~]#cd mod_bw
修改mod_bw 0.7的bug:
由於mod_bw 0.7在编译时bug,会导致在安装后出现apr_atomic_add找不到的错误,所以要修改mod_bw.c

将原本的:

/* Compatibility for ARP < 1 */
#if (APR_MAJOR_VERSION < 1)
#define apr_atomic_inc32 apr_atomic_inc
#define apr_atomic_dec32 apr_atomic_dec
#define apr_atomic_add32 apr_atomic_add
#define apr_atomic_cas32 apr_atomic_cas
#define apr_atomic_set32 apr_atomic_set
#endif


改成:

/* Compatibility for ARP < 1 */
/*
#if (APR_MAJOR_VERSION < 1)
#define apr_atomic_inc32 apr_atomic_inc
#define apr_atomic_dec32 apr_atomic_dec
#define apr_atomic_add32 apr_atomic_add
#define apr_atomic_cas32 apr_atomic_cas
#define apr_atomic_set32 apr_atomic_set
#endif
*/
[root@localhost mod_bw]# /usr/sbin/apxs -i -a -c mod_bw.c
编辑http.conf,并在你要控制的带宽目录上加上:
<Directory "/">
# Limit BW usage
BandWidthModule On
ForceBandWidthModule On
#开放20KB对外使用
Bandwidth all 20480
#每个Connection最小带宽为2K
MinBandwidth all 2048
#大于50M的文件传输速率按50Kbytes/sec
#LargeFileLimit 51200 51200
#最大同时连接数量
#MaxConnection 300
</Directory> 

重启apache2

[root@localhost html]# apachectl configtest
Syntax OK
[root@localhost html]# apachectl restart


[编辑] 建立受保护的目录

[root@http ~]# cd /etc/httpd/conf.d/
[root@http conf.d]# ls
php.conf  proxy_ajp.conf  README  welcome.conf
[root@http conf.d]# vi protect.conf
[root@http conf.d]# apachectl restart
[root@http conf.d]# cd /var/www/html/disk2/
[root@http disk2]# vi .htaccess
[root@http disk2]# cat .htaccess 
AuthName        "Protoct test by .htaccess"
Authtype        Basic
AuthUserFile    /var/www/apache.passwd
require user test
[root@http disk2]# htpasswd -c /var/www/apache.passwd test
New password: 
Re-type new password: 
Adding password for user test
[root@http disk2]# cd /var/www/
[root@http www]# cat apache.passwd 
test:mHphkRULvOzYM

[编辑] 让服务器支持安全 HTTP 协议( HTTPS )

[root@localhost ~]# openssl genrsa -out ca.key 1024
Generating RSA private key, 1024 bit long modulus
..............................++++++
.....++++++
e is 65537 (0x10001)

[root@localhost ~]# openssl req -new -key ca.key -out ca.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:Guangdong
Locality Name (eg, city) [Newbury]:Guangzhou
Organization Name (eg, company) [My Company Ltd]:www.15099.net
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:www.15099.net
Email Address []:lihaixin@15099.net

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@localhost ~]#

[root@localhost ~]# openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt
Signature ok
subject=/C=CN/ST=Guangdong/L=Guangzhou/O=www.15099.net/CN=www.15099.net/emailAddress=lihaixin@15099.net
Getting Private key
[root@localhost ~]#

把生成的三个文件移动到相应的目录

mv ca.crt /etc/pki/tls/certs
mv ca.key /etc/pki/tls/private/ca.key
mv ca.csr /etc/pki/tls/private/ca.csr

编辑ssl.conf文件,把认证文件目录修改上面复制过去的路径

vi +/SSLCertificateFile /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key

配置虚拟主机/etc/httpd/conf.d/VirtualHost.conf文件

NameVirtualHost *:443

<VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile /etc/pki/tls/certs/ca.crt
        SSLCertificateKeyFile /etc/pki/tls/private/ca.key
        <Directory /var/www/vhosts/15099.net/httpsdocs>
        AllowOverride All
        </Directory>
        DocumentRoot /var/www/vhosts/15099.net/httpsdocs
        ServerName www.15099.net
</VirtualHost>

新建立https跟目录

mkdir -p /var/www/vhosts/15099.net/httpsdocs
touch /var/www/vhosts/15099.net/httpsdocs/index.html
ecsho “test https” > /var/www/vhosts/15099.net/httpsdocs/index.html

测试是否生效,访问https://www.15099.net出现如下画面 Image:Centos-https-conf-001.jpg

单击“或者,您可以添加一个例外...”弹出如下图

Image:Centos-https-conf-002.jpg

获取证书,确认安全例外,这样就自动把数字证书导入本地系统,可以打开网页了

Image:Centos-https-conf-003.jpg

[编辑] 开放一般用户的网页发布权限

[root@linux ~]# vi /etc/httpd/conf/httpd.conf
# 找到如下的设定项目,大约在 340 行左右∶
<IfModule mod_userdir.c>
    UserDir disable
    #UserDir public_html
</IfModule>
# 将他改成如下的情况喔!
<IfModule mod_userdir.c>
    #UserDir disable
    UserDir public_html
</IfModule>

# 重新启动一下先!
[root@linux ~]# /etc/init.d/httpd restart

假设你的主机有个 wiki 的用户,那么此用户如何建立他的个人首页? 我们这样测试一下好了!(底下的工作请以你欲建立个人网页帐号身份使用者处理)∶

[wiki@linux ~]$ cd ~
[wiki@linux ~]$ mkdir public_html
[wiki@linux ~]$ chmod 755 public_html
[wiki@linux ~]$ chmod 755 ~
[wiki@linux ~]$ cd public_html
[wiki@linux public_html]$ echo "Test your home" >> index.html

这样你就可以使用 http://你的主机名称/~wiki/ 访问了

[编辑] 虚拟主机的构建

新建vi /etc/httpd/conf.d/VirtualHost.conf文件,内容如下,可根据自己的需要田间虚拟主机

NameVirtualHost *:80
<VirtualHost *:80>
         ServerAdmin mobile@15099.net
         DocumentRoot /var/www/html/ecshop/ecshopv260
         ServerName gzctc.dns0755.net
         ErrorLog logs/gzctc.dns0755.net-error_log
         CustomLog logs/gzctc.dns0755.net-access_log common
</VirtualHost>

[编辑] Servlet/JSP服务器的构建(Tomcat5)

[编辑] webalizer日志分析工具

安装

[root@http www]# yum -y install webalizer
Loading "fastestmirror" plugin
Loading mirror speeds from cached hostfile
 * base: 192.168.3.13
 * updates: 192.168.3.13
 * addons: 192.168.3.13
 * extras: 192.168.3.13
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package webalizer.i386 0:2.01_10-30.1 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size 
=============================================================================
Installing:
 webalizer               i386       2.01_10-30.1     base              105 k

Transaction Summary
=============================================================================
Install      1 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total download size: 105 k
Downloading Packages:
(1/1): webalizer-2.01_10- 100% |=========================| 105 kB    00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: webalizer                    ######################### [1/1] 

Installed: webalizer.i386 0:2.01_10-30.1
Complete!
[root@http www]# 

另外还有一个比较好的统计工具,比google分析还强大,地址:http://www.phpmyvisites.net/

[编辑] 基于日志的站点统计系统的构建( Awstats )

[编辑] 参考资料

如何安装Bandwidth Mod: http://100000.myabc.cn/home/Blog/view/16665.htm

个人工具