LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 753|回复: 0

在FreeBSD5.4Release下架设WEBFTP服务器

[复制链接]
发表于 2005-10-5 11:54:01 | 显示全部楼层 |阅读模式
在FreeBSD5.4Release下架设WEBFTP服务器
Auther:royce
http://zjnt.3322.org
Email:royce0725@163.com
Data:2005-09-24

本文中所涉及的软件可以到我的网站,一些细微变动也将在我的wiki上修改
http://zjnt.3322.org


需求分析:需要APACHE2+PHP4.X+MYSQL提供WEB功能,支持PHP(FASTCGI),为WEB用户提供FTP服务和虚拟主机服务,并创建一个FTP帐号用于下载服务,限制下载速度为200K,单线程。服务器软件采用port安装,采用默认路径保存,考虑到日志和 mysql数据库,将/var分区分大大点,现在大家的硬盘动辄100G,不少这些吧,web和ftp默认目录为/home下的用户目录文件,ftp下载帐号目录为/ftp,单独一个分区,所以要将分完区后所有的空间都分配给/home。系统支持ssh远程操作。


下图为服务器所在的网络环境:



机器配置:
CPU:CY1.1图拉丁
内存:384M
硬盘:双硬盘,a:40G,b:30G,其中b划分为/ftp,和Swap两个区
网卡:Inter21143,系统里识别为dc0 :: dc0=192.168.0.224/32


FreeBSD一些设置路径: FreeBSD设置是非常简单的,一般开机要启动的选项都放在/etc/rc.conf文件里,打开这个文件你将看到一些服务和网卡的启动选项,IP地址等也可以在这里面直接修改,启动服务一般都是xxx_enable=”YES”这样的格式,xxx名字一般决定于/usr/local/etc/rc.d/目录下的脚本,比如你安装了Apache2后下面就有个apache2.sh脚本,只要在rc.conf文件里添加这么一句:apache2_enable=”YES”,那么系统开机的时候自然就会启动了。系统的inetd服务设置文件是/etc/inetd.conf文件,这些文件都可以直接用文本编辑器修改,比如ee,vi等,非常方便。



基本操作系统的安装


最小化安装系统。

安装编译系统必须的原代码,当最小化安装完操作系统后,选configure->distributions->src->ALL,这里选择安装所有源代码是为了方便编译内核等操作,不然有时候会出现莫名其妙的错误,所以建议选择ALL。当基本的东西都安装完后先别退出,然后配置在NetWorking->interface,配置并激活你的网卡,打开NetWorking->sshd服务,这样可以在windows上用ssh软件连接远程操作,能复制粘贴哦。最好在这里直接添加个用户,也可以进系统后用adduser添加,SSH2?连接默认是不能用ROOT连接的,而我们后面的操作都可以在windows下远程操作完成,哪个方便我们就用哪个,不是吗?win下的连接程序有SecureCRT?和Putty,Putty是免费的程序,小巧但功能强大,SecureCRT?是商业程序我论坛里有破解版的下。如果你要上传软件的话,可以暂时打开NetWorking->inetd服务,将它配置文件里的头行tcp4 ftp那行取消注释,这样就可以用本地用户直接登陆FTP上传了,当然,在后面我们装完vsftp后要将他关掉的。

重新启动进入系统,永远的第一件事:passwd,修改密码

后面的操作全部可以在win下操作完成,比较方便,如果你想速度快点的话。下图为SecureCRT?的操作界面:
http://zjnt.3322.org/picture/SecureCRT.JPG


下面正式开始BSD路程 (后面我将全部采用SSH操作)

进入系统修改密码后先要安装cvsup,5.4R安装cvsup要两张盘换来换去的,所以采用在线自动安装:

pkg_add -r cvsup-without-gui          #-r 选项的意思就是在网络上自动下载安装软件包


升级源码和port树

cp /usr/src/share/examples/cvsup/standard-supfile ports-supfile /tmp


修改2个文件,将里面的host:修改成本地地址host:cvsup.freebsdchina.org然后进行安装

cd /tmp
cvsup -g -L2 standard-supifle;cvsup -g -L2 ports-supfile
上面cvsup命令中的分号“;”代表执行完第一条命令后再继续执行第二条,这里要蛮久的,放一起执行了,出去逛一下。


更新完后先别急,先升级系统编译内核,至于编译内核,论坛里多了,我就不多说了,只是要注意几点,如果要打开服务器的防火墙功能和磁盘限额功能的话,务必将选项加入内核文件里。具体的make world过程,请看Freebsd使用手册,有详细操作介绍,本页左侧有连接:

cp /usr/src/sys/i386/conf/GENERIC /usr/src/mykernel
cd /boot
cp -R kernel kernel.old               #备份旧内核,以防万一
cd /usr/src
make kernel KERNCONF=mykernel


安装mysql

cd /usr/ports/databases/mysql41-server/


修改Makefile文件,在Makefile里CONFIGURE_TARGET那行前面的空行加上两行优化选项:

BUILD_OPTIMIZED=yes
BUILD_STATIC= yes


然后就可以安装了,很简单,程序会自动下载缺少的包:

make install clean

下面进行mysql的基本配置,好让mysql能正常启动:

cp /usr/local/share/mysql/my-medium.cnf /etc/my.cnf         #mysql和WEB结合的基本配置
chown -R mysql /var/db/mysql                                #权限,不然启动不了mysql的
/usr/local/bin/mysql_install_db --user=root                 #安装默认数据
/usr/local/etc/rc.d/mysql-server.sh start                   #启动mysql


现在执行mysql看看可以启动了没

mysql

如果出现这样的字母,就代表你成功了

           
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7 to server version: 4.1.14-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>


输入quit退出吧。下面修改一下mysql的root密码

#mysqladmin -u root password 'xxxxx'         #修改默认密码,一定要改,XXX里添入你的密码


一切都搞定,加入rc.conf,让系统启动时自动开启服务,在/etc/rc.conf最后加入这么一句

mysql_enable="YES"

安装Apache2

先介绍下Apache2的几条常用命令,apache的几个常用命令如下

/usr/local/sbin/apachectl start          #启动Apache2
/usr/local/sbin/apachectl stop           #停止Apache2
/usr/local/sbin/apachectl restart        #重启Apache2
httpd –t                                #测试httpd.conf配置文件语法有无出错


安装APACHE2?的时候可能会提示安装新版textproc/expat2错误,我们先把旧版卸载了再装Apache就行

cd /usr/ports/textproc/expat2
make deinstall
make install
cd /usr/ports/www/apache2
make install                    #freebsd就是这么可爱


安装fastcgi

cd  ../mod_fastcgi
make WITH_APACHE2=yes install clean


安装php

cd ../mod_php4
make install clean                         #这里make的时候会跳出个选项让你选,记住不要选debug就行

cd /usr/local/etc
cp php.ini-recommeded php.ini              #为php复制配置文件


安装php4-extensions(一定要装,不然安装很多PHP程序都会出错,最经典的出错方式是页面一片空白)

cd /usr/ports/lang/php4-extensions/
make install clean                         #看看自己又什么需要的就选上吧,默认的不要动就可以。


下面就修改httpd.conf文件,其实根据安装完php等mod时的提示就可以了,先备份下文件

cd /usr/local/etc/apache2/
cp httpd.conf httpd.conf.bak


修改配置文件

ee httpd.conf


在AddType? application/x-gzip .gz .tgz后面加上下面2行

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps


利用ee编辑器的搜索功能找到DirectoryIndex? index.html index.html.var这行,加入index.php,修改后如下

DirectoryIndex index.php index.html index.html.var


搜索ServerName?这一行,修改成自己域名。

ServerName localhost:80


好了,这下修改完成,现在可以按ctrl+c保存退出,启动Apache2,如果能看到默认页就代表成功了

/usr/local/sbin/apachectl start


再写个PHP测试页,查看PHP能用了没

ee /usr/local/www/data/index.php加入以下两句:
<?
phpinfo();
?>


然后保存退出,在浏览器里运行,比如http://xxx/index.php 如果测试页面出来了,那么恭喜你,PHP已成功运行了。


虚拟主机配置:

搞了很久,同一个问题,设置好虚拟主机后访问的时候,不管用哪一个域名访问都是默认的第一个目录,解决办法

1 注释掉DocumentRoot那一行.
2 NameVirtualHost *:80                     //我试过设置成自己的IP那就是访问同一个目录
3 下面虚拟主机按照默认的配置就行


由于我们要建立多个虚拟主机,如果不将默认主机注释掉的话,那么apache默认就是访问default servers,所以我们将他注释掉,并将它作为第一个虚拟主机添加就行了

下面添加默认主机,用的默认路径

<VirtualHost *:80>
ServerName xxx.org
DocumentRoot /usr/local/www/data
</virtualHost>


这样就行了,下面我们添加一个其他目录作为虚拟主机路径

<VirtualHost *:80>
ServerName xxx.3322.org
DocumentRoot /home/web
<Directory />
AllowOverride None
Order Deny,Allow
Deny from all
</Directory>
<Directory "/home/web">
Options Indexes FollowSymLinks
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</virtualHost>


这时我们在浏览器里输入新建的地址,如果没有index.*文件,浏览器会出现整个目录所有文件,所以如果你不想让别人看到你目录下的文件时,可以将Options Indexes FollowSymLinks?里的Indexes去掉,这样就看不到目录下文件了。


如果要增加虚拟主机,只要把上面这一段复制一下,改一下两个路径和域名就行了在/etc/rc.conf里加入让系统启动的时候自动启动APACHE2?服务:

apache2_enable="YES"


这样系统启动后就会自动启动apache2.现在可以试下,重新启动apache2,然后用虚拟主机的域名访问下就OK了,到此,apache基本上就搞定了,以后有什么需求比如验证目录可以以后再加。


VSFTP的安装

安装VSFTP的时候就要将原本系统自带的FTP关掉,编辑/etc/rc.conf文件,inetd_enable=”YES”删除掉或者注释掉,修改/etc/inetd.conf,将ftp那行重新注释掉,保存退出,OK,关掉现在正在运行的FTP服务,等下要测试VSFTP的,运行命令:

killall –HUP inetd


运行netstat命令查看,如果没有21端口了,证明已经关掉FTP服务,就可以开始安装VSFTP。

netstat –net


FTP SERVER建议根据情况来选择,如果你的用户多而且管理需求比较严格那么我建议用pureftp+mysql,我的服务器需求不高,只有我和两个朋友用,加上一个FTP用户提供下载服务,限制速度,所以我采用了VSFTP,这是个不仅安全,速度也很快的FTP服务器,它更适合于用在匿名FTP服务器上,很多大的站点提供的FTP下载都是基于他的,下面就先来安装吧:

cd /usr/ports/ftp/vsftpd/
make install clean


安装完后配置VSFTP的配置文件

cd /usr/local/etc
cp vsftpd.conf.dist vsftpd.conf


下面就可以开始设置vsftp的配置文件,按照自己的需求设置,下面是我的配置文件



  1. # Example config file /etc/vsftpd.conf
  2. #
  3. # Allow anonymous FTP? (Beware - allowed by default if you comment this out).
  4. #
  5. #这个是允许匿名用户进入,我用不着,所以注释掉了
  6. #anonymous_enable=YES

  7. # Uncomment this to allow local users to log in.
  8. #允许本地用户登陆,因为我只有几个用户登陆,所以就用本地用户来验证,只要把他们的shell设置成nologin就
  9. #可以了,比较安全的做法是用mysql或者其他数据库做虚拟用户验证
  10. local_enable=YES

  11. # Uncomment this to enable any form of FTP write command.
  12. #允许用户在自己的目录里写入,重命名等权限
  13. write_enable=YES

  14. # Default umask for local users is 077. You may wish to change this to 022,
  15. # if your users expect that (022 is used by most other ftpd's)
  16. #设定本地用户上载文件的 umask 值,默认就好了
  17. local_umask=022

  18. # Uncomment this to allow the anonymous FTP user to upload files. This only
  19. # has an effect if the above global write enable is activated. Also, you will
  20. # obviously need to create a directory writable by the FTP user.
  21. # 允许匿名用户上传
  22. #anon_upload_enable=YES

  23. # Uncomment this if you want the anonymous FTP user to be able to create
  24. # new directories.
  25. # 允许匿名用户对目录操作有创建目录和写的权限
  26. #anon_mkdir_write_enable=YES

  27. # Activate directory messages - messages given to remote users when they
  28. # go into a certain directory.
  29. #打开信息提示
  30. dirmessage_enable=YES

  31. # Activate logging of uploads/downloads.
  32. #xferlog_enable=YES

  33. # Make sure PORT transfer connections originate from port 20 (ftp-data).
  34. #用于控制在服务器端, 是否使用端口20(ftp-data)进行数据联接
  35. connect_from_port_20=YES

  36. # If you want, you can arrange for uploaded anonymous files to be owned by
  37. # a different user. Note! Using "root" for uploaded files is not
  38. # recommended!
  39. #chown_uploads=YES
  40. #chown_username=whoever

  41. # You may override where the log file goes if you like. The default is shown
  42. # below.
  43. #xferlog_file=/var/log/vsftpd.log

  44. # If you want, you can have your log file in standard ftpd xferlog format
  45. #采用标准ftpd日志格式
  46. xferlog_std_format=YES

  47. # You may change the default value for timing out an idle session.
  48. #限制远程最大命令间隔时间,默认就行
  49. idle_session_timeout=600

  50. # You may change the default value for timing out a data connection.
  51. #设定空闲的数据连接所允许的最大时长
  52. data_connection_timeout=120

  53. # It is recommended that you define on your system a unique user which the
  54. # ftp server can use as a totally isolated and unprivileged user.
  55. #nopriv_user=ftpsecure
  56. #
  57. # Enable this and the server will recognise asynchronous ABOR requests. Not
  58. # recommended for security (the code is non-trivial). Not enabling it,
  59. # however, may confuse older FTP clients.
  60. #async_abor_enable=YES
  61. #
  62. # By default the server will pretend to allow ASCII mode but in fact ignore
  63. # the request. Turn on the below options to have the server actually do ASCII
  64. # mangling on files when in ASCII mode.
  65. # Beware that turning on ascii_download_enable enables malicious remote parties
  66. # to consume your I/O resources, by issuing the command "SIZE /big/file" in
  67. # ASCII mode.
  68. # These ASCII options are split into upload and download because you may wish
  69. # to enable ASCII uploads (to prevent uploaded scripts etc. from breaking),
  70. # without the DoS risk of SIZE and ASCII downloads. ASCII mangling should be
  71. # on the client anyway..
  72. #ascii_upload_enable=YES
  73. #ascii_download_enable=YES

  74. # You may fully customise the login banner string:
  75. 设置FTP远程连接时显示的欢迎信息
  76. ftpd_banner=Welcome to zjnt.3322.org FTP service.

  77. # You may specify a file of disallowed anonymous e-mail addresses. Apparently
  78. # useful for combatting certain DoS attacks.
  79. #deny_email_enable=YES
  80. # (default follows)
  81. #banned_email_file=/etc/vsftpd.banned_emails
  82. #
  83. # You may specify an explicit list of local users to chroot() to their home
  84. # directory. If chroot_local_user is YES, then this list becomes a list of
  85. # users to NOT chroot().
  86. #chroot_list_enable=YES
  87. # (default follows)
  88. #chroot_list_file=/etc/vsftpd.chroot_list
  89. #
  90. # You may activate the "-R" option to the builtin ls. This is disabled by
  91. # default to avoid remote users being able to cause excessive I/O on large
  92. # sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
  93. # the presence of the "-R" option, so there is a strong case for enabling it.
  94. #ls_recurse_enable=YES

  95. #下面是重点添加
  96. #将VSFTP设置成独立运行模式,处在一直监听状态,port是你想要的端口,默认为21并打开tcp_wrappers
  97. listen=YES
  98. listen_port=21
  99. tcp_wrappers=YES


  100. #锁定本地用户的根目录为他们自己的目录,这个设置很重要,一定要打开
  101. chroot_local_user=YES

  102. #如果被激活, 并且 vsftpd 以 "listen" 模式启动, vsftpd 将会background 监听进程
  103. background=YES

  104. #限制FTP服务器最大连接数
  105. max_clients=50

  106. #限制每个IP最大连接数,一般设置成2为好,1的话经常如果不小心掉线再连接就连接不上了
  107. max_per_ip=2

  108. #本地用户最大下载速度为1024K*200=200KB,你想限制多少就乘多少好了
  109. local_max_rate=204800

  110. #打开单个用户配置文件,可设置每个用户的权限
  111. user_config_dir=/usr/local/etc/vsftpd_user_conf

  112. #这个为安全选项,设置成YES后,无论哪个用户连接到FTP都显示为FTP
  113. hide_ids=YES

  114. secure_chroot_dir=/usr/local/share/vsftpd/empty
复制代码



配置好后保存退出,运行FTP服务器

/usr/local/libexec/vsftpd /usr/local/etc/vsftpd.conf


检查一下看看21端口有没有被打开

netstat –net


OK,端口处在listen状态了,下面就用FTP连上去看看,如果你在win下,建议用LeaFTP?,*nix下建议用lftp,在本机上直接连接,出现如下提示:

  1. ftp localhost
  2. Trying ::1...
  3. ftp: connect to address ::1: Connection refused
  4. Trying 127.0.0.1...
  5. Connected to localhost.myweb.com.
  6. 220 Welcome to zjnt.3322.org FTP service.
  7. Name (localhost:xxx):
复制代码
出现这个提示,代表FTP已安装成功了,输入本地用户名和密码直接进入,如果要添加一个新FTP用户,只要增加一个本地用户就行,当然,一些地方要小小的修改一下,比如不能给他shell等,如下:
  1. adduser
  2. Username: example    #用户名
  3. Full name: #用户全名,空着也没关系
  4. Uid (Leave empty for default): 2010   #用户UID,我的FTPuser都是从2000开始的
  5. Login group [example]:  #用户参加的组
  6. Login group is example. Invite example into other groups? []:
  7. Login class [default]:
  8. Shell (sh csh tcsh bash nologin) [sh]: nologin    #这个最为重要,用户登陆的shell为nologin
  9. Home directory [/home/example]: #用户目录所在
  10. Use password-based authentication? [yes]:
  11. Use an empty password? (yes/no) [no]: yes   #密码设置为空,这里我是举例,你们当然用默认的NO
  12. Lock out the account after creation? [no]:   
  13. Username   : example
  14. Password   : <blank>
  15. Full Name  :
  16. Uid        : 2010
  17. Class      :
  18. Groups     : example
  19. Home       : /home/example
  20. Shell      : /usr/sbin/nologin
  21. Locked     : no
  22. OK? (yes/no):y
复制代码



输入YES后就建立了用户,重新连接FTP,看看用这个用户登陆了没?没错吧?哈哈,距离目标又近了一步。下面我们就要限制FTP用户的某些权利了,比如download用户只能下载不能上传,upload用户只能上传不能下载,呵呵,不知道大家还记得vsftp.conf文件里最后我们添加的那句选项吗?对,就是

user_config_dir=/usr/local/etc/vsftpd_user_conf
这句,后面的/usr/local/etc/vsftpd_user_cnf可以自己设置目录,比如/etc/vsftpd_user_cnf,然后建立这个目录,输入命令
mkdir /usr/local/etc/vsftpd_user_conf


建立好目录后就可以针对用户开始进行设置,先用上面的方法增加两个用户upload,download,然后进行权限限制

ee /usr/local/etc/vsftpd_user_conf/upload


在里面添加这么一句:

write_enable=YES
download_enable=NO


这样就可以写入,并限制了下载,同样,针对download用户修改如下:

ee /usr/local/etc/vsftpd_user_conf/download
write_enable=NO


配置好后就只能下载了。安装到这里,FTP就差不多了,但是还有很多问题我没解决,比如如何在/usr/local/etc/vsftpd_user_conf下的用户限制文件里限制单个用户的下载速度而不用在vsftpd.conf文件里限制所有用户的速度,在chinaunix论坛里的斑竹说虚拟用户限制的选项就是限制匿名用户速度的那个选项,改天试一下再修改吧。最后,在/etc/rc.conf文件里加入启动选项

vsftpd_enable=”YES”
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表