程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 在CentOS上搭建LAMP+vsftpd環境的簡單指南,centosvsftpd

在CentOS上搭建LAMP+vsftpd環境的簡單指南,centosvsftpd

編輯:關於PHP編程

在CentOS上搭建LAMP+vsftpd環境的簡單指南,centosvsftpd


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 [email protected]
...
#系統更新
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

# 因為剛剛安裝了 PHP,所以別忘了重啟一下 Apache,否則 PHP 不能正常工作
service httpd restart

然後浏覽器中訪問 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 服務器安裝完成。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved