一、前言
centOS的WWW服务器配置,在上次对服务器进行维护的时候算是经历过了一番。用的是centOS5.4,历时绝对不超过半天。相关的教程在网上其实一搜就有,而且还是不少。合理利用google和百度才是王道。
身在教育网,一些资源无法访问到是比较无奈的。很多国外的站点提供的好东西都DOWN不到。sougou浏览器对教育网用户来说是一大福音,至少让我们看到了; 而前几天下载了opera发现能利用它浏览到一些国外站点,前提是opera turbo启动的状态下。然而turbo并不是一个代理(假如没有理解错的话,只是一个压缩网页的东东,很明显的是开启opera turbo的情况下图片会模糊),那么或许说教育网访问不了国外站点或许原因是因为下载速度太慢而导致连接超时从而造成页面无法访问?
写到这里忽然发现严重跑偏…..且回到正题。
二、配置
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.save
wget http://centos.ustc.edu.cn/CentOS-Base.repo.5
mv CentOS-Base.repo.5 CentOS-Base.repo (中国科学技术大学)
[root@sample ~]# yum -y install httpd ← 在线安装httpd
[root@sample ~]# yum -y install php ← 在线安装PHP
(以下资料大部分出自centOS Pub, 还有部分搜索自网上)
【Apace基本配置】
[root@sample ~]# vi /etc/httpd/conf/httpd.conf ← 编辑Apache的配置文件
ServerTokens OS ← 找到这一行,将“OS”改为“Prod”(在出现错误页的时候不显示服务器操作系统的名称) ↓
ServerTokens Prod ← 变为此状态
ServerSignature On ← 找到这一行,将“On”改为“Off” ↓
ServerSignature Off ← 在错误页中不显示Apache的版本
ServerAdmin root@localhost ← 将管理员邮箱设置为自己常用的邮箱 ↓
ServerAdmin yourname@yourserver.com ← 根据实际情况修改默认设置
#ServerName new.host.name:80 ← 修改主机名 ↓
ServerName www.centospub.com:80 ← 根据实际情况修改,端口号保持默认的80
Options Indexes FollowSymLinks ← 找到这一行,删除“Indexes”,并添加“Includes”、“ExecCGI” ↓
Options Includes ExecCGI FollowSymLinks ← 允许服务器执行CGI及SSI
#AddHandler cgi-script .cgi ← 找到这一行,去掉行首的“#”,并在行尾添加“.pl” ↓
AddHandler cgi-script .cgi .pl ← 允许扩展名为.pl的CGI脚本运行
AllowOverride None ← 找到这一行,将“None”改为“All” ↓
AllowOverride All ← 变为此状态,允许.htaccess
LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”" combined ← 找到这一行 ↓
LogFormat “%h %l %u %t \”%!414r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”" combined ← 改为此状态(添加“!414”到规则中,对于过长的日志不记录)
<Directory “/var/www/icons”> ← 找到这一个标签,并在标签中更改相应选项
Options Indexes MultiViews ← 找到这一行,将“Indexes”删除 ↓
Options MultiViews ← 变为此状态(不在浏览器上显示树状目录结构)
AllowOverride None
Order allow,deny
Allow from all
</Directory>
【Apache虚拟服务器配置】
#NameVirtualHost *:80 ← 找到这一行,去掉行首的# ↓
NameVirtualHost *:80 ← 变为此状态
<VirtualHost *:80>
ServerAdmin yourname@yourserver.com
DocumentRoot /var/www/html ← www.centospub.com所定义的规则
ServerName www.centospub.com
ErrorLog logs/error_log
CustomLog logs/access_log combined
</VirtualHost>
【apache一般用户网页权限】
[root@sample ~]# vi /etc/httpd/conf/httpd.conf ← 用vi打开SSH的配置文件
UserDir disable ← 找到这一行,在行首增加“#”
#UserDir disable ← 修改后变为此状态
#UserDir public_html ← 找到这一行,去掉行首的“#”
UserDir public_html ← 修改后变为次状态
找到下面水平线之间的部分行,将每行行首的“#”去掉,并在一些细节选项上按提示做修改
——————————————————————————-
#<Directory /home/*/public_html>
# AllowOverride FileInfo AuthConfig Limit ← 找到此行,将相应选项删除后,赋予All选项
AllowOverride All ← 变为此状态,允许使用.htaccess
# Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec ← 找到此行,将相应选项删除
Options IncludesNoExec ExecCGI FollowSymLinks ← 变为此状态,允许执行CGI及SSI
# <Limit GET POST OPTIONS>
# Order allow,deny
# Allow from all
# </Limit>
# <LimitExcept GET POST OPTIONS>
# Order deny,allow
# Deny from all
# </LimitExcept>
#</Directory>
——————————————————————————-
↓ 以上水平线之间的部分修改后,变为如下状态,尤其注意不要忘记将没行行首的“#”去掉!请核对。
root@sample ~]# chmod 711 /home/centospub/ ← 将欲开放Web的用户目录属性设置为711
[root@sample ~]# su – centospub ← 登录为欲开放Web的一般用户
[root@sample ~]# cd ← 到用户根目录
[centospub@sample ~]$ mkdir public_html ← 在用户根目录下建立 Web目录,名为“public_html”
[centospub@sample ~]$ chmod 755 public_html ← 将用Web目录的属性设置为755
然后,让每建立新用户的时候,用户目录下都能自动建立Web开放用的public_html目录,使得每个用户都有开放Web的机会。
[centospub@sample ~]# su – ← 再次登录为root用户
Password: ← 在这里输入密码
[root@sample ~]# mkdir /etc/skel/public_html ← 建立相应目录,从而使得public_html自动建立
[root@sample ~]# exit ← 退出root登录,返回一般用户(这里以返回centospub用户登录为例)
【Apache测试】
[root@sample ~]# echo “My site is www.centospub.com ” >> /var/www/html/index.html ← 建立www.centospub.com指向/var/www/html/下的测试页
[root@sample ~]# echo “My site is digeast.no-ip.com” >> /home/centospub/public_html/index.html ← 建立digeast.no-ip.com指向/home/centospub/public_html/下的测试页
然后分别在浏览器输入两个域名,如果每次都能够显示出相应域名的页面,说明虚拟主机已正常工作。
[root@sample ~]# rm -f /var/www/html/index.html ← 删除测试用的遗留文件
[root@sample ~]# rm -f /home/centospub/public_html/index.html ← 删除测试用的遗留文件
[root@sample ~]# chkconfig httpd on ← 设置HTTP服务自启动
[root@sample ~]# chkconfig –list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 确认2–5为on的状态就OK
[root@sample ~]# /etc/rc.d/init.d/httpd start ← 启动HTTP服务
Starting httpd: [ OK ] ← 启动成功会出现OK
如果启动失败的话,会出现错误信息。原因可能是因为httpd.conf文件编辑过程中的失误,请检查httpd.conf。
[root@sample ~]# echo hello >> /var/www/html/index.html ← 建立测试页
[root@sample ~]# rm -f /var/www/html/index.html ← 删除测试页
[1] 对HTML格式网页正确显示的测试
[root@sample ~]# vi /var/www/html/index.html ← 建立测试页,内容如下:
<html><head><meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″><title>Hello,World!</title><body>Hello,World!</body></html>
[root@sample ~]# rm -f /var/www/html/index.html ← 删除刚刚测试用的网页
[2] 对CGI的支持进行测试
[root@sample ~]# vi /var/www/html/test.cgi ← 建立CGI测试页,内容如下:
#!/usr/bin/perl
print “Content-Type: text/html\n\n”;
print “<html><body>”;
print “Hello,World!CGI is working!<br>”;
print “</body></html>”;
[root@sample ~]# chmod 755 /var/www/html/test.cgi ← 然后将CGI测试文件属性设置为755
然后在浏览器中输入“http://服务器IP地址/test.cgi”或者“http://你的域名/test.cgi”,如果正确显示“Hello,World!CGI is working!”,说明对于CGI的支持没有问题。
[3] 对PHP的支持进行测试
[root@sample html]# vi /var/www/html/test.php ← 建立PHP测试文件,内容如下:
<?php
phpinfo();
?>
然后在浏览器中输入“http://服务器IP地址/test.php”或者“http://你的域名/test.php”后,正确的显示出了服务器上PHP的详细信息,说明对PHP可以正确的支持。
[4] 对SSI进行测试
[root@sample ~]# vi /var/www/html/test.shtml ← 建立SSI测试页,内容如下:
<html><head><meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″><title>Hello,World!</title><body>
TEST SSI<!–#config timefmt=”%Y/%m/%d %H:%M:%S” –><!–#echo var=”DATE_LOCAL” –></body></html>
然后在浏览器中输入“http://服务器IP地址/test.shtml”或者“http://你的域名/test.shtml”,如果正确显示当时的日期和时间,说明对于SSI的支持没有问题。
[5] 对.htaccess的支持进行测试
[root@sample ~]# vi /var/www/html/index.shtml ← 建立.htaccess测试用的页,内容如下:
<html><head><meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″><title>Hello,World!</title><body>The name of the file is <!–#echo var=”DOCUMENT_NAME” –></body></html>
然后在浏览器中输入“http://服务器IP地址”或者“http://你的域名”,如果显示“Forbidden”,说明.htaccess正常。
然后建立一个.htaccess文件,并定义相应规则,如下:
[root@sample html]# vi /var/www/html/.htaccess ← 建立.htaccess文件,内容如下:
DirectoryIndex index.shtml
这时,再次在浏览器中输入“http://服务器IP地址”或者“http://你的域名”,如果正确显示“ The name of the file is index.shtml”,说明.htaccess中的规则生效状态,OK。
[6] 删除测试用的遗留文件
[root@sample html]# rm -f /var/www/html/* /var/www/html/.htaccess ← 删除测试用过的遗留文件
【MYSQL安装及PHP-mysql模块】
[root@sample ~]# yum -y install mysql-server ← 安装MySQL
[root@sample ~]# yum -y install php-mysql ← 安装php-mysql
【mysql配置】
[root@sample ~]# vi /etc/my.cnf ← 编辑MySQL的配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1 ← 找到这一行,在这一行的下面添加新的规则,让MySQL的默认编码为UTF-8
default-character-set = utf8 ← 添加这一行
然后在配置文件的文尾填加如下语句:
[mysql]
default-character-set = utf8
[root@sample ~]# chkconfig mysqld on ← 设置MySQL服务随系统启动自启动
[root@sample ~]# chkconfig –list mysqld ← 确认MySQL自启动
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 如果2–5为on的状态就OK
[root@sample ~]# /etc/rc.d/init.d/mysqld start ← 启动MySQL服务
Initializing MySQL database: [ OK ]
Starting MySQL: [ OK ]
[root@sample ~]# mysql -u root ← 用root用户登录MySQL服务器
mysql> set password for root@localhost=password(‘在这里填入root密码’); ← 设置root密码
[root@localhost ~]# mysql -u root -h sample.centospub.com -p ← 通过空密码用root登录
mysql> grant all privileges on test.* to centospub@localhost identified by ‘在这里定义密码’; ← 建立对test数据库有完全操作权限的名为centospub的用户
mysql> revoke all privileges on *.* from centospub@localhost; ← 取消centospub用户对数据库的操作权限
mysql> delete from mysql.user where user=”; ← 删除匿名用户
mysql> show databases; ← 查看系统已存在的数据库
mysql> create database test; ← 建立名为test的数据库
mysql> drop database test; ← 删除名为test的空数据库
mysql> use test ← 连接到数据库
mysql> show tables; ← 查看数据库中已存在的表
mysql> create table test(num int, name varchar(50)); ← 在数据库中建立表
mysql> insert into test values(1,’Hello World!’); ← 插入一个值到表中
mysql> update test set name=’Hello Everyone!’; ← 更新表的信息,赋予新的值
mysql> delete from test where num=1; ← 删除表内的值
mysql> drop table test; ← 删除表
[root@sample ~]# /etc/rc.d/init.d/httpd restart ← 重新启动HTTP服务
【SSH服务器端配置】
[root@sample ~]# vi /etc/ssh/sshd_config ← 用vi打开SSH的配置文件
#Protocol 2,1 ← 找到此行将行头“#”删除,再将行末的“,1”删除,只允许SSH2方式的连接
Protocol 2 ← 修改后变为此状态,仅使用SSH2
#ServerKeyBits 768 ← 找到这一行,将行首的“#”去掉,并将768改为1024 ↓
ServerKeyBits 1024 ← 修改后变为此状态,将ServerKey强度改为1024比特
#PermitRootLogin yes ← 找到这一行,将行首的“#”去掉,并将yes改为no ↓
PermitRootLogin no ← 修改后变为此状态,不允许用root进行登录
#PasswordAuthentication yes ← 找到这一行,将yes改为no ↓
PasswordAuthentication no ← 修改后变为此状态,不允许密码方式的登录
#PermitEmptyPasswords no ← 找到此行将行头的“#”删除,不允许空密码登录
PermitEmptyPasswords no ← 修改后变为此状态,禁止空密码进行登录
【SSH过滤IP段】
[root@sample ~]# vi /etc/hosts.deny ← 修改屏蔽规则,在文尾添加相应行
sshd: ALL ← 添加这一行,屏蔽来自所有的SSH连接请求
[root@sample ~]# vi /etc/hosts.allow ← 修改允许规则,在文尾添加相应行
sshd: 192.168.0. ← 添加这一行,只允许来自内网的SSH连接请求
[root@sample ~]# /etc/rc.d/init.d/sshd restart ← 重新启动SSH服务器
[root@sample ~]# su – centospub ← 登录为一般用户centospub
[centospub@sample ~]$ ssh-keygen -t rsa ← 建立公钥与私钥
Enter file in which to save the key (/home/kaz/.ssh/id_rsa): ← 钥匙的文件名,这里保持默认直接回车
Created directory ‘/home/kaz/.ssh’
Enter passphrase (empty for no passphrase): ← 输入口令
Enter same passphrase again: ← 再次输入口令
Your identification has been saved in /home/kaz/.ssh/id_rsa.
Your public key has been saved in /home/kaz/.ssh/id_rsa.pub.
The key fingerprint is:
tf:rs:e3:7s:28:59:5s:93:fe:33:84:01:cj:65:3b:8e centospub@sample.centospub.com
然后确认一下公钥与密钥的建立,以及对应于客户端的一些处理。
[centospub@sample ~]$ cd ~/.ssh ← 进入用户SSH配置文件的目录
[centospub@sample .ssh]$ ls -l ← 列出文件
total 16
-rw——- 1 centospub centospub 951 Sep 4 19:22 id_rsa ← 确认私钥已被建立
-rw-r–r– 1 centospub centospub 241 Sep 4 19:22 id_rsa.pub ← 确认公钥已被建立
[centospub@sample .ssh]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys ← 公钥内容输出到相应文件中
[centospub@sample .ssh]$ rm -f ~/.ssh/id_rsa.pub ← 删除原来的公钥文件
[centospub@sample .ssh]$ chmod 400 ~/.ssh/authorized_keys ← 将新建立的公钥文件属性设置为400
然后,将私钥通过安全的方式转移到欲通过SSH连接到服务器的PC上。这里,以通过3.5寸磁盘为介质为例:
centospub@sample .ssh]$ exit ← 退出一般用户的登录(返回root的登录)
[root@sample ~]# mount /mnt/floppy/ ← 加载软盘驱动器
[root@sample ~]# mv /home/centospub/.ssh/id_rsa /mnt/floppy/ ← 将刚刚建立的私钥移动到软盘
[root@sample ~]# umount /mnt/floppy/ ← 卸载软盘驱动器
【vsftp】
本地用户经过设置后可以进行ftp访问。而匿名用户的访问经过了转换,在系统中。匿名用户的用户名为ftp, 系统将其属性设置为 根目录 /var/ftp/, 禁止控制台登陆,也就是,该用户只能进行ftp访问。
FreeBSD下vsftpd 的执行程序为 /usr/local/libexec/vsftpd, 一般情况下调用 /usr.local/libexec/vsftpd & 即可启动VSFTPD, 注意,修改 /usr.local/etc/vsftpd.conf文件中的listen要设置为YES.
VSFTPD有两种开机自启动模式: inet模式和standalone模式,推荐使用standalone模式。 两种模式的启动方法依次为:
1. inet模式
修改 /etc/inet.conf 修改或添加,使以下行有效:
ftp stream tcp nowait root /usr/sbin/tcpd /usr/local/sbin/vsftpd
并修改 /usr.local/etc/vsftpd.conf文件,使 listen=NO
2. standalone模式
修改/etc/inet.conf文件并注释掉vsftpd的启动行。
修改 /usr.local/etc/vsftpd.conf文件,使 listen=YES
在 /usr/local/etc/rc.d/下添加vsftpd 的启动脚本
VSFTPD的基本设置
VSFTPD的配置文件为/usr.local/etc/vsftpd.conf, 有多个选项,下面一一说明:
Anonymous_enable=yes
允许匿名登陆, 设置为yes后,ftp用户和anonymous用户都被认为以匿名登陆
Dirmessage_enable=yes
切换目录时,显示目录下.message的内容, 可以在vsftpd.conf文件中通过message_file修改文件名
Local_umask=022
FTP上本地的文件权限,默认是077, 前面有0认为是8进制,无0认为是十进制
Connect_from_port_20=yes
启用FTP数据端口的数据连接
Xferlog_enable=yes
激活上传和下传的日志
Xferlog_std_format=yes
使用标准的日志格式
Ftpd_banner=XXXXX
欢迎信息
Pam_service_name=vsftpd
验证方式 *
Listen=yes
独立的VSFTPD服务器 *
Anon_upload_enable=yes
开放上传权限
Anon_mkdir_write_enable=yes
可创建目录的同时可以在此目录中上传文件
Write_enable=yes
开放本地用户写的权限
Anon_other_write_enable=yes
匿名帐号可以有删除的权限
Anon_world_readable_only=no
放开匿名用户浏览权限
Ascii_upload_enable=yes
启用上传的ASCII传输方式
Ascii_download_enable=yes
启用下载的ASCII传输方式
Banner_file=/var/vsftpd_banner_file
用户连接后欢迎信息使用的是此文件中的相关信息
Idle_session_timeout=600(秒)
用户会话空闲后10分钟
Data_connection_timeout=120(秒)
将数据连接空闲2分钟断
Accept_timeout=60(秒)
将客户端空闲1分钟后断
Connect_timeout=60(秒)
中断1分钟后又重新连接
Local_max_rate=50000(bite)
本地用户传输率50K
Anon_max_rate=30000(bite)
匿名用户传输率30K
Pasv_min_port=50000
将客户端的数据连接端口改在
Pasv_max_port=60000
50000—60000之间
Max_clients=200
FTP的最大连接数
Max_per_ip=4
每IP的最大连接数
Listen_port=5555
从5555端口进行数据连接
Local_enble=yes
本地帐户能够登陆
Write_enable=no
本地帐户登陆后无权删除和修改文件
下面这是一组
<!–[if !supportEmptyParas]–> <!–[endif]–>
Chroot_local_user=yes
本地所有帐户都只能在自家目录
Chroot_list_enable=yes
文件中的名单可以调用
Chroot_list_file=/任意指定的路径/vsftpd.chroot_list
前提是chroot_local_user=no
这又是一组
<!–[if !supportEmptyParas]–> <!–[endif]–>
Userlist_enable=yes
在指定的文件中的用户不可以访问
Userlist_deny=yes
<!–[if !supportEmptyParas]–> <!–[endif]–>
Userlist_file=/指定的路径/vsftpd.user_list
<!–[if !supportEmptyParas]–> <!–[endif]–>
又开始单的了
<!–[if !supportEmptyParas]–> <!–[endif]–>
Banner_fail=/路径/文件名
连接失败时显示文件中的内容
Ls_recurse_enable=no
<!–[if !supportEmptyParas]–> <!–[endif]–>
Async_abor_enable=yes
<!–[if !supportEmptyParas]–> <!–[endif]–>
one_process_model=yes
<!–[if !supportEmptyParas]–> <!–[endif]–>
Listen_address=10.2.2.2
将虚拟服务绑定到某端口
Guest_enable=yes
虚拟用户可以登陆
Guest_username=所设的用户名
将虚拟用户映射为本地用户
User_config_dir=/任意指定的路径/为用户策略自己所建的文件夹
指定不同虚拟用户配置文件的路径
又是一组
<!–[if !supportEmptyParas]–> <!–[endif]–>
Chown_uploads=yes
改变上传文件的所有者为root
Chown_username=root
<!–[if !supportEmptyParas]–> <!–[endif]–>
又是一组
<!–[if !supportEmptyParas]–> <!–[endif]–>
Deny_email_enable=yes
是否允许禁止匿名用户使用某些邮件地址
Banned_email_file=//任意指定的路径/xx/
<!–[if !supportEmptyParas]–> <!–[endif]–>
又是单的
<!–[if !supportEmptyParas]–> <!–[endif]–>
Pasv_enable=yes
服务器端用被动模式
User_config_dir=/任意指定的路径//任意文件目录
指定虚拟用户存放配置文件的路径
3. 虚拟用户的设置
所谓虚拟用户,就是在本地用户中不存在,而又可以远程登陆的ftp用户。一般多采用pam 认证方式。 本节只考虑使用pam_pwdfile.so库文件进行认证。更多的可能采用的是 mysql数据库进行认证。
基本的设置过程如下。
在 vsftp.conf中进行如下设置使 vsftpd 知道采用的是 pam的认证方式。
Guest_enable=YES
Guest_username=virtual # 该用户是系统的本地用户
Pam_service_name=vsftpd # 该文件指明pam认证对应的配置文件,默认存储位置/etc/pam.d/
配置 pam认证,复制 /etc/pam.d/ftpd 为 vsftpd
在vsftpd文件前加入以下两句
auth sufficient /lib/security/pam_pwdfile.so pwdfile=/etc/vsftpd_login
account sufficient pam_permit.so
注: 这样做是为了使本地用户与远程用户都能够登入
pam_pwdfile.so 系统默认没有安装,进入ports/security/ 进行安装,注意安装后的so文件与以上文件路径
生成 /etc/vsftpd_login 文件, 该文件是口令文件,记录了用户名和口令的hash编码
编辑虚拟用户名及口令文件 /root/vsftp.login, 格式如下
用户名:口令
生成以下perl脚本 fileter.pl
###########################
#! /usr/bin/perl -w
use strict;
# filter “user:cleartext” lines into “user:md5_crypted”
# probably requires glibc
while (<>) {
chomp;
(my $user, my $pass) = split /:/, $_, 2;
my $crypt = crypt $pass, ‘$1$’ . gensalt(8);
print “$usercrypt”;
}
sub gensalt {
my $count = shift;
my @salt = (‘.’, ‘/’, 0 .. 9, ‘A’ .. ‘Z’, ‘a’ .. ‘z’);
my $s;
$s .= $salt[rand @salt] for (1 .. $count);
return $s;
}
shell下运行 perl /root/fileter.pl /root/vsftp.login > /etc/vsftpd_login删除vsftp.login, 注意vsftpd_login最好不要带文件类型,pam认证好像忽略类型名
在vsftpd.conf中修改为:
User_config_dir = /usr/loca/etc/virtual/ # 需手工mk
以各用户名为文件名生成各文件,设置用户的访问权限。语法与 vsftpd.conf中相同