在CentOS上搭建LAMP+vsftpd环境的简单指南
这篇文章主要介绍了在CentOS上搭建LAMP+vsftpd环境的简单指南,这也是当下服务器的最流行配置方案之一,需要的朋友可以参考下
VPS 可以看成是一台只有你一个人使用的服务器(事实上它是一个虚拟机),你可以在上面安装任何软件,拥有最大的权限。正所谓权限越大,责任越大,你需要自行安装 Web 服务器,数据库,PHP,还有其它一些维护工作都要自行处理。
现在大多数 VPS 提供的操作系统都是 Linux,而且是没有图形界面的的,只提 SSH 命令行接口,所以需要会一些简单的 Linux 命令行。Linux 又有众多的发行版,最好的发行版可能是 Redhat,但它是商业软件,不能免费使用,不过好在它还有一个社区版本 CentOS,完全采用 Redhat 的源代码,去掉 Redhat 的 LOGO,替换成自己的,另外去掉一些闭源软件,所以系统功能、性能及稳定性几乎等同于 Redhat,就选它了。
安装 Linux
对于 Linux 的安装而言,你可以选择你所熟悉的发行版如 Ubuntu、Debian、Fedora 等,服务商会以最小化安装方式默认装好,我选择的版本是 CentOS 6.3,考虑到 VPS 内存较小,安装的是 32 位版本。
安装好以后以以 root 用户登陆上去,并且让系统进行一些必要的更新。Linux 和 Mac 都自带了 Terminal,如果是 Windows,建议使用 PuTTY 来进行 SSH 连接。
- #以 root 用户登陆服务器
 - ssh root@198.xxx.xxx.xxx
 - ...
 - #系统更新
 - yum update
 - ...
 
安装 Apache
Apache 是一款 Linux 平台上老牌的免费开源 Web 服务器,据说全世界超过一半的网站都是跑在 Apache 上的。要安装 Apache,在命令行下输入以下命令:
yum install httpd
默认安装的 Apache 可能不是最新版,但确是在此 Linux 版本上经过测试的最稳定版本,如果你一定需要安装最新版,则需从 Apache 官网上去下载最新版。
安装好后,执行以下命令启动 Apache 服务:
service httpd start
默认的网页存放目录位于/var/www/html/,然后在浏览器中访问 http://198.xxx.xxx.xxx,如果可以出现 Apache 的一个测试页面,那么说明 Apache 已安装成功。
安装 MySQL
MySQL 是一款非常流行的数据库软件,最初由瑞典 MySQL AB 公司所开发,后被 Sun 公司收购,目前为 Oracle 公司旗下产品,安装 MySQL 的命令如下:
yum install mysql-server
启动 MySQL 服务:
service mysqld start
然后需要为 MySQL 的 root 用户设置一个密码,可输入一下命令:
/usr/bin/mysql_secure_installation
执行以上命令的话,MySQL 会要求你提供现在 root 用户的密码,因为我们刚刚装好,所以密码是空的,直接回车,然后设置新的 root 用户密码。
紧接着还会有一些安全选项要你选择 Y 还是 N。例如,是否移除匿名登陆,是否阻止 root 用户从远程登陆,如果选择 y ,那么 root 只能以 localhost 方式登陆,另外还有是否移除 test 数据库、立即刷新权限表等,大概情况如下:
- [root@CentOS6 ~]# /usr/bin/mysql_secure_installation
 - NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
 - SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
 - In order to log into MySQL to secure it, we'll need the current
 - password for the root user. If you've just installed MySQL, and
 - you haven't set the root password yet, the password will be blank,
 - so you should just press enter here.
 - Enter current password for root (enter for none):
 - OK, successfully used password, moving on...
 - Setting the root password ensures that nobody can log into the MySQL
 - root user without the proper authorisation.
 - Set root password? [Y/n] y
 - New password:
 - Re-enter new password:
 - Password updated successfully!
 - Reloading privilege tables..
 - ... Success!
 - By default, a MySQL installation has an anonymous user, allowing anyone
 - to log into MySQL without having to have a user account created for
 - them. This is intended only for testing, and to make the installation
 - go a bit smoother. You should remove them before moving into a
 - production environment.
 - Remove anonymous users? [Y/n] y
 - ... Success!
 - Normally, root should only be allowed to connect from 'localhost'. This
 - ensures that someone cannot guess at the root password from the network.
 - Disallow root login remotely? [Y/n] y
 - ... Success!
 - By default, MySQL comes with a database named 'test' that anyone can
 - access. This is also intended only for testing, and should be removed
 - before moving into a production environment.
 - Remove test database and access to it? [Y/n] y
 - - Dropping test database...
 - ... Success!
 - - Removing privileges on test database...
 - ... Success!
 - Reloading the privilege tables will ensure that all changes made so far
 - will take effect immediately.
 - Reload privilege tables now? [Y/n] y
 - ... Success!
 - Cleaning up...
 - All done! If you've completed all of the above steps, your MySQL
 - installation should now be secure.
 - Thanks for using MySQL!
 
安装 PHP
PHP 是一个被广泛使用的开源动态脚本语言,要安装 PHP,并使其与 MySQL 协同工作,需执行以下命令:
yum install php php-mysql
此时需要测试 PHP 是否能正常工作,可以建一个测试页。
- #切换到 Apache 默认网页目录
 - cd /var/www/html
 - #创建一个 php 脚本文件
 - touch phpinfo.php
 - #向文件写入一小段 php 脚本,测试用
 - echo '<?php phpinfo(); ?>' > phpinfo.php
 - <p># 因为刚刚安装了 PHP,所以别忘了重启一下 Apache,否则 PHP 不能正常工作<br data-filtered="filtered">service httpd restart</p>
 
然后浏览器中访问 http://198.xxx.xxx.xxx/phpinfo.php,看 PHP 是否已经正常工作。
如果该页面能正常显示服务器相关环境信息,说明 LAMP 环境已经可以正常工作了。
安装 vsftpd
要安全地上传文件到服务器,或者从服务器上下载文件,最简便的方式是用 FTP,这里我们选择 Linux 下非常流行的 “Very Secure FTPD”,即非常安全的 FTP:
yum install vsftpd
安装好后,还要进行一些简单的配置:
- #编辑 vsftpd 配置文件
 - vi /etc/vsftpd/vsftpd.conf
 - ...
 - #不允许匿名登陆
 - anonymous_enable=NO
 - #本地账户可以登陆
 - local_enable=YES
 - #可以写入
 - write_enable=YES
 - #所有用户只能访问其 home 目录
 - chroot_local_user=YES
 - ...
 - #重启 vsftpd 以上设置才能生效
 - service vsftpd restart
 
如何以 FTP 协议访问服务器呢,这里推荐 FileZilla 这个 FTP 客户端工具,有 Windows 版本、Linux 版本以及 Mac OS 版本。
登陆 vsftpd 一般用 Linux 用户区登陆,但是不允许用 root 用户登陆,所以,需要另外新建一个 Linux 用户:
- #添加用户 lichao
 - adduser lichao
 - #为 lichao 设置密码
 - passwd lichao
 - #如果出于安全考虑,这个用户你只想它能登陆 vsftpd,
 - #而不能以 ssh 方式登陆服务器,可以禁止其 ssh 登陆
 - usermod -s /sbin/nologin lichao
 
至此,就可以用任何 FTP 工具如 FileZilla,以 lichao 这个用户及对应的密码来来登陆 vsftpd 了,默认的目录是 /home/lichao
设置 Apache、MySQL 和 vsftpd 服务开机启动
设置它们开机启动的命令如下:
chkconfig httpd on
chkconfig mysqld on
chkconfig vsftpd on
PHP 会随 Apache 一起启动。
至此,一个基本完整的动态网页服务器、数据库服务器、FTP 服务器安装完成。
