linux配置php運行環境

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

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

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

注意選擇的公共鏡像是:CentOS 6.5 64位版
買完後,登錄你的賬號,查找購買的服務器的公網ip:
這裏寫圖片描述

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

ssh root@公網IP
  • 1
  • 1

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

好了,登錄成功後,下一步就開始安裝軟件了,我們需要安裝的軟件有apache,php和MySQL

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

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

vi /etc/selinux/config
#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增加
:wq!#保存退出
shutdown -r now#重啓系統
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然後安裝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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

安裝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,直接覆蓋即可)
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

然後爲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 #重啓
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

最後一步,安裝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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

OK,到這裏,所有的軟件都安裝完了,現在測試一下。
默認的,你會有一個www的文件夾,裏面有個html的文件夾,你的默認訪問路徑就是這裏。

cd /var/www/html
vi index.php #編輯輸入下面內容
<?php
echo "hello my server!";
?>
:wq! #保存退出
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章