1. 程式人生 > >阿里雲ECS伺服器Linux環境下配置php伺服器(一)--基礎配置篇

阿里雲ECS伺服器Linux環境下配置php伺服器(一)--基礎配置篇

最近在搞一個後臺,想建一個php的環境,記錄一下過程。

首先是買伺服器,為了省錢其實剛開始可以不買,在自己的機器上先搭建一個本地伺服器,通過內網ip測試,等開發差不多後,再買,然後把環境移到ecs伺服器上。

買哪種型別的ECS伺服器比較好?
我主要是為了把csdn的部落格移到自己的主頁上,同時業餘時間學習一下php,不需要太大的空間和頻寬,所以我就買的最便宜的68元/月的,包年680。
如下:
這裡寫圖片描述
這裡寫圖片描述

注意選擇的公共映象是:CentOS 6.5 64位版
買完後,登入你的賬號,查詢購買的伺服器的公網ip:
這裡寫圖片描述

然後開啟你的終端(windows系統請cmd開啟控制檯),輸入:

ssh
root@公網IP

注意,root其實是你的登入名,這個在你購買伺服器的時候已經填寫過了,公網ip就是上面說的ip。
然後輸入密碼,就登入進來了,密碼也在你購買的時候設定過。
這裡寫圖片描述

好了,登入成功後,下一步就開始安裝軟體了,我們需要安裝的軟體有apache,php和mysql。

ps:如果你購買的是北京的伺服器,有個安全組需要設定,我全部用的預設設定,暫時還沒發現會有什麼影響。

首先關閉SELINUX(SELINUX是一個安全子系統,它能控制程式只能訪問特定檔案。如果不關閉,你可能訪問檔案受限):

vi /etc/selinux/config
#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉 SELINUX=disabled #增加 :wq!#儲存退出 shutdown -r now#重啟系統

然後安裝apache

yum install httpd #根據提示,輸入Y安裝即可成功安裝
/etc/init.d/httpd start#啟動Apache
備註:Apache啟動之後會提示錯誤:
正在啟動 httpd:httpd: Could not reliably determine the server's fully qualif domain name, using ::1 for ServerName
解決辦法:
vi /etc/httpd/conf/httpd.conf #編輯
找到 #ServerName www.example.com:80 修改為 ServerName www.jbaobao.net:80 #這裡設定為你自己的域名,如果沒有域名,可以設定為localhost :wq! #儲存退出 chkconfig httpd on #設為開機啟動 /etc/init.d/httpd restart #重啟Apache

安裝mysql

yum install mysql mysql-server #詢問是否要安裝,輸入Y即可自動安裝,直到安裝完成
/etc/init.d/mysqld start #啟動MySQL
chkconfig mysqld on #設為開機啟動
cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #拷貝配置檔案(注意:如果/etc目錄下面預設有一個my.cnf,直接覆蓋即可)

然後為mysql的root賬號設定密碼(預設的是空)

mysql_secure_installation回車

這裡需要注意的是,如果你是新安裝的mysql,會彈出如下提示:
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): 
大概意思就是如果你是新安裝的話,你的預設密碼是空,直接按enter鍵就可以了

然後設定新的密碼,輸入兩次。

再然後,會有若干個提示:
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!
大概意思是,mysql會預設建立一個匿名使用者,方便你測試什麼的,現在問你要不要刪掉它,果斷刪掉


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!
大概意思是,root使用者預設只能訪問localhost,以防止有人猜你的密碼。。。問你是否禁止root登陸,也選yes,雖然基本上不會有人來猜吧。。。


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
大概意思是,mysql預設建立了一個名為test的資料庫,這個庫任何人都可以訪問,問你是不是要把它刪掉,也刪掉。


Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y

大概意思是,上面所有的修改是否馬上生效,選y

總之就是一路Yes。。。。
最後出現:Thanks for using MySQL!

MySql密碼設定完成,重新啟動 MySQL:
/etc/init.d/mysqld restart #重啟

最後一步,安裝php

yum install php #根據提示輸入Y直到安裝完成
#安裝PHP元件,使 PHP5 支援 MySQL
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt #這裡選擇以上安裝包進行安裝,根據提示輸入Y回車
/etc/init.d/mysqld restart #重啟MySql
/etc/init.d/httpd restart #重啟Apche

OK,到這裡,所有的軟體都安裝完了,現在測試一下。
預設的,你會有一個www的資料夾,裡面有個html的資料夾,你的預設訪問路徑就是這裡。

cd /var/www/html
vi index.php #編輯輸入下面內容
<?php
echo "hello my server!";
?>
:wq! #儲存退出

然後在瀏覽器裡鍵入你的公網ip,順利的話,你就開啟自己第一個頁面了!~