在CentOS上搭建LAMP+vsftpd環境的簡單指南_php技巧

來源:互聯網
上載者:User

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 currentpassword for the root user. If you've just installed MySQL, andyou 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 MySQLroot user without the proper authorisation.Set root password? [Y/n] yNew password:Re-enter new password:Password updated successfully!Reloading privilege tables.. ... Success!By default, a MySQL installation has an anonymous user, allowing anyoneto log into MySQL without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures 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 canaccess. This is also intended only for testing, and should be removedbefore 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 farwill 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 MySQLinstallation 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 使用者:

#添加使用者 lichaoadduser lichao#為 lichao 設定密碼passwd lichao#如果出於安全考慮,這個使用者你只想它能登陸 vsftpd,#而不能以 ssh 方式登陸伺服器,可以禁止其 ssh 登陸usermod -s /sbin/nologin lichao

至此,就可以用任何 FTP 工具如 FileZilla,以 lichao 這個使用者及對應的密碼來來登陸 vsftpd 了,預設的目錄是 /home/lichao
設定 Apache、MySQL 和 vsftpd 服務開機啟動

設定它們開機啟動的命令如下:

chkconfig httpd onchkconfig mysqld onchkconfig vsftpd on

PHP 會隨 Apache 一起啟動。

至此,一個基本完整的動態網頁伺服器、資料庫伺服器、FTP 伺服器安裝完成。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.