一、Apache服务
Apache是Apache软件基金会的一个开放源码的网页服务器,是世界使用最广泛的Web服务端之一,译为阿帕奇;Apache具有优秀的性能,稳定性,是通过加载模块来提供各种功能。
1、主配置httpd.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
ServerRoot
ServerAdmin
user
group
ServerName
DocumentRoot
Listen
PidFile
DirectoryIndex
ErrorLog
CustomLog
LogLevel
Timeout
KeepAlive
MaxKeepAliveRequests
KeepAliveTimeout
Include
<Directory />
……
< /Directory >
|
#工作模式(默认Prefork)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
< /IfModule >
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
< /IfModule >
|
2、虚拟主机配置
1>.基于域名虚拟主机
每个网站使用不同域名,对应IP地址和TCP端口相同,也是使用最多的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@www ~]
[root@www ~]
[root@www ~]
[root@www ~]
ServerName 192.168.0.200:80
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot /var/www/test1 .com
ServerAlias test1.com
ErrorLog "/var/www/logs/test1.com-error_log"
CustomLog "/var/www/logs/test1.com-access_log" common
< /VirtualHost >
<VirtualHost *:80>
ServerName www.test2.com
DocumentRoot /var/www/test2 .com
ServerAlias test2.com
ErrorLog "/var/www/logs/test2.com-error_log"
CustomLog "/var/www/logs/test2.com-access_log" common
< /VirtualHost >
[root@www ~]
设置权限
[root@www~]
[root@www~]
|
http://www.test1/2.com #测试成功,没有dns服务器情况下,客户端需要做host记录
2>.基于IP虚拟主机
每个网站使用不同域名,不同IP,相同TCP端口 (配置多个IP,eth0:0表示网卡的第二个IP地址,依次0、1、2等添加更多IP地址)
1
2
3
4
5
6
7
8
9
10
|
[root@www~]
[root@www network-scripts]
[root@www network-scripts]
DEVICE=eth0:0
TYPE=Ethernet
ONBOOT= yes
BOOTPROTO=static
IPADDR=192.168.0.200
GATEWAY=192.168.0.1
NETMASK=255.255.255.0
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@www ~]
NameVirtualHost *:80
<VirtualHost 192.168.0.200:80>
ServerName www.test1.com
DocumentRoot /var/www/test1 .com
ServerAlias test1.com
ErrorLog "/var/www/logs/test1.com-error_log"
CustomLog "/var/www/logs/test1.com-access_log" common
< /VirtualHost >
<VirtualHost 192.168.0.201:80>
ServerName www.test2.com
DocumentRoot /var/www/test2 .com
ServerAlias test2.com
ErrorLog "/var/www/logs/test2.com-error_log"
CustomLog "/var/www/logs/test2.com-access_log" common
< /VirtualHost >
[root@www ~]
|
3>.基于端口虚拟主机
每个网站相同域名,相同IP,不同端口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@www ~]
NameVirtualHost *:80
Listen 8080
Listen 8081
<VirtualHost *:8080>
ServerName www.test1.com
DocumentRoot /var/www/test1 .com
ServerAlias test1.com
ErrorLog "/var/www/logs/test1.com-error_log"
CustomLog "/var/www/logs/test1.com-access_log" common
< /VirtualHost >
<VirtualHost *:8081>
ServerName www.test2.com
DocumentRoot /var/www/test2 .com
ServerAlias test2.com
ErrorLog "/var/www/logs/test2.com-error_log"
CustomLog "/var/www/logs/test2.com-access_log" common
< /VirtualHost >
[root@www ~]
|
3、基于用户的身份验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@www ~]
<VirtualHost *:80>
DocumentRoot /var/www/test .com
ServerName www. test .com
ServerAlias test1.com
ErrorLog "/var/www/logs/test.com-error_log"
CustomLog "/var/www/logs/test.com-access_log" common
<Directory "/var/www/html" >
AuthName "Please input Password"
AuthType basic
AuthUserFile /etc/httpd/ . passwd
Require valid-user
< /Directory >
< /VirtualHost >
|
4、创建用户及密码,此处密码与用户的系统密码无关
1
2
3
4
5
|
[root@www ~]
New password:
Re- type new password:
Adding password for user user
[root@www ~]
|
5、基于客户端地址访问控制,主要应用在区域<Directory >……</Directory>
Order配置项,定义控制顺序
1>.先允许后拒绝,默认拒绝所有:Order allow,deny
2>.先拒绝后允许,默认允许所有:Order deny,allow
Allow、Deny配置项,设置允许或拒绝的IP地址、主机名
1>.Deny from address1 address2 …
2>.Allow from address1 address2 …
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@www ~]
NameVirtualHost 192.168.0.200
<VirtualHost *:80>
DocumentRoot /var/www/test .com
ServerName www. test .com
ServerAlias test1.com
ErrorLog "/var/www/logs/test.com-error_log"
CustomLog "/var/www/logs/test.com-access_log" common
<Directory "/var/www/html" >
order allow,deny
allow from 192.168.1.0 /24
< /Directory >
< /VirtualHost >
[root@www ~]
|
6、Apache下301转向代码(需要开启mod_rewrite)
1>.将不带www的域名转向到带abc的域名下
1
2
3
4
|
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www. test .com [NC]
RewriteRule ^(.*)$ http: //abc .www. test .com
|
2>.重定向到新域名
1
2
3
|
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http: //www . test .com/$1 [L,R=301]
|
3>.使用正则进行301转向,网站实现伪静态
1
2
3
|
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+)\.html$ news.php? id =$1
|
7、Apache下虚拟主机配置301跳转
为实现URL规范化,SEO通常将不带www的域名转向到带www域名,vhosts.conf中配置为:
1
2
3
4
|
ServerName www. test .com
DocumentRoot /var/www/html
ServerName test .com
RedirectMatch permanent ^/(.*) http: //www . test .com/$1
|
二、Nginx服务
Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理服务器,也是一个 IMAP/POP3/SMTP代理服务器
1、apache与nginx优缺点
1>.apache对客户端的响应是支持并发的,运行守护进程后,会产生多个子进程/线程,每个子进程/线程分别对客户端请求进行响应。
2>.apache可以提供静态和动态服务,通常使用php模块来实现,mod_php5或叫apxs2。
3>.apache缺点:工作方式是基于多进程模式,每个用户请求就会创建一个子进程/线程来响应,如果并发请求非常多的时候,就会占用极多的系统资源CPU和内存,因此在并发量大情况下,apache并不是优势。
4>.nginx稳定性和低系统资源消耗的特点,并发连接的高处理能力(单台物理服务器可支持30000~50000个并发请求),nginx采用异步服务器模式,异步服务器事件驱动模式,用户并发请求只需一个进程或几个线程,因此占用的系统资源也相对非常少,比如,10000的并发连接请求,nginx只需要几十M的内存,而apache可能需要几百M的内存。
5>.nginx没有内置的模块来对PHP进行支持,而是通过第三方FastCGI模块来支持处理。
2、安装Nginx
1
2
3
4
5
6
7
8
|
[root@www ~]
[root@www ~]
[root@www ~]
[root@www ~]
[root@www nginx-1.4.2]
--user=nginx --group=nginx --with-http_stub_status_module
[root@www nginx-1.4.2]
[root@www nginx-1.4.2]
|
3、常用命令
1
2
3
|
nginx -t :检测配置文件语法是否正确
nginx :启动nginx服务
killall -9 nginx :关闭nginx服务
|
4、编写nginx启动 停止 重启等SysV管理脚本,方便使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[root@www ~]
PROG= "/usr/local/nginx/sbin/nginx"
PIDF= "/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
echo "Nginx service start success."
;;
stop)
kill -s QUIT $( cat $PIDF)
echo "Nginx service stop success."
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $( cat $PIDF)
echo "reload Nginx config success."
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
[root@www ~]
[root@www ~]
[root@www ~]
|
5、主配置文件nginx.conf
全局配置
1
2
3
4
|
user nobody;
worker_processes 1;
error_log logs /error .log;
pid logs /nginx .pid;
|
I/O事件配置
1
2
3
|
events {
worker_connections 1024;
}
|
HTTP配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
http {
include mime.types;
default_type application /octet-stream ;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"' ;
access_log logs /access .log main;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
charset utf8;
access_log logs /host .access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404 .html;
error_page 500 502 503 504 /50x .html;内部错误返回页面
location = /50x .html {
root html;
}
}
}
|
6、虚拟主机配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@www nginx]
include /usr/local/nginx/vhost/ *.conf;
[root@www nginx]
server {
listen 80;
server_name www.test1.com test1.com;
charset utf8;
access_log logs /test1 .com.log main;
location / {
root /usr/local/nginx/html/test1 .com;
index index.html index.htm;
}
}
|
7、Nginx域名重定向
1
2
3
4
5
6
7
|
server {
listen 80;
server_name www. test .com test .com;
location / {
rewrite "^/(.*)$" http: //bj . test .com;
}
}
|
8、Nginx网站目录跳转域名
1
2
3
4
5
6
|
server {
listen 80;
server_name www. test .com test .com;
location / {
rewrite ^ /Login/login .html http: //www . test .com;
}
|
9、Nginx虚拟目录并设置用户认证
1
2
3
4
5
6
7
8
9
10
11
|
location ~ /stats {
root /usr/local/nginx/html/ ;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/ $fastcgi_script_name;
include fastcgi_params;
}
auth_basic "说明信息" ;
auth_basic_user_file /usr/local/nginx/ .htpasswds;
}
|
10、Nginx伪静态配置(wordpress或discuz)
1
2
3
4
5
6
7
|
server {
listen 80;
server_name www.yngx.net yngx.net;
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index .php?p=$1 last;
}
|
11、配置FastCGI支持PHP网页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[root@www ~]
[root@www php-5.4]
[root@www php-5.4]
-with-config- file -path= /usr/local/php/etc \
-with-gd --with-zlib --with-iconv -- enable -zip -- enable -pdo \
- enable -xml --with-openssl --with-curl -- enable -bcmath -- enable - ftp \
- enable -mbstring -- enable -fpm --with-fpm-user=nginx \
-with-fpm-group=nginx
[root@www php-5.4]
[root@www php-5.4]
[root@www php-5.4]
[root@www php-5.4]
[root@www nginx]
[root@www nginx]
[root@www nginx]
user = nginx
group = nginx
pid = run /php-fpm .pid
pm.start_server = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
[root@www php-5.4]
[root@www php-5.4]
[root@www php-5.4]
location ~ \.php$ { root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html $fastcgi_script_name;
include fastcgi_params;
}
|
测试:
1
2
3
4
|
[root@www nginx]
<? php
phpinfo();
?>
|
三、AWStats+JAWStats日志分析工具
Awstats是一款基于Perl的WEB日志分析工具,而JAWStats是一款基于PHP的统计套件,旨在为Awstats提供更为精美图形界面。
JAWStats 是一个用来统计网站日志的工具,在 AWStats 的基础上生成更加清晰的关于网站访问数据的图表、表格。
它可以统计您站点的如下信息:
1>.访问量,访问次数,页面浏览量,点击数,数据流量等
2>.精确到每月、每日、每小时的数据
3>.访问者国家
4>.访问者IP
5>.Robots/Spiders的统计
6>.访客持续时间
7>.对不同Files type 的统计信息
8>.Pages-URL的统计
9>.访客操作系统浏览器等信息
10>.其它信息(搜索关键字等等)
1、安装配置AWStats
下载:http://www.awstats.org/#DOWNLOAD
1
2
3
4
5
6
7
|
[root@www ~]
[root@www ~]
[root@www ~]
[root@www ~]
[root@www ~]
[root@www ~]
[root@www tools]
|
1>.输入 none 然后回车
2>.输入 y 确认创建配置文件
3>.输入配置文件名称,一般输入域名。
4>.配置文件使用默认路径 /etc/awstats
5>.按回车继续
6>.按回车完成配置文件的创建
1
2
3
4
5
6
7
8
|
[root@www tools]
LogFile=LogFile= "/var/log/httpd/access_log"
DirData= "/var/lib/awstats"
[root@www tools]
[root@www tools]
[root@www tools]
[root@www tools]
* * /1 * * * /usr/local/awstats/tools/awstats_updateall .pl now
|
添加apache支持awstats参数
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@www ~]
Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/local/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"
<Directory "/usr/local/awstats/wwwroot" >
Options None
AllowOverride None
Order allow,deny
Allow from all
< /Directory >
[root@www ~]
[root@www ~]
|
测试访问:http://192.168.0.102/awstats/awstats.pl?config=www.test.com

2、安装配置JAWStats
下载:http://www.jawstats.com/download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[root@www ~]
[root@www ~]
[root@www ~]
[root@www jawstats]
[root@www jawstats]
<?php
// core config parameters
$sDefaultLanguage = "zh-cn" ;
$sConfigDefaultView = "thismonth.all" ;
$bConfigChangeSites = true ;
$bConfigUpdateSites = true ;
$sUpdateSiteFilename = "xml_update.php" ;
// individual site configuration
$aConfig[ "www.test.com" ] = array(
"statspath" => "/var/lib/awstats" ,
"updatepath" => "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" ,
"siteurl" => "http://www.test.com" ,
"sitename" => "www.test.com" ,
"theme" => "default" ,
"fadespeed" => 250,
"password" => "123456" ,
"includes" => "" ,
"language" => "zh-cn"
);
?>
|
3、添加中文语言包
下载:http://www.jawstats.com/src/languagepack.zip
1
2
3
4
|
[root@www jawstats]
[root@www languages]
[root@www languages]
[root@www languages]
|
4、配置虚拟目录用户认证访问
1
2
3
4
5
6
7
8
9
|
[root@www ~]
Alias /tongji "/usr/local/jawstats"
<Directory "/usr/local/jawstats" >
Options Indexes MultiViews
AuthType Basic
AuthName "Please Login"
AuthUserFile /etc/httpd/ . passwd
Require valid-user
< /Directory >
|
5、测试
http://www.test.com/tongji