在雲服務器盛行的今天,人手一個服務器已經不再是夢想,而云服務器的環境配置對於一些剛接觸的朋友們來說還是有一定難度的。所以今天我們一起來看一下,如何手動配置AMP
。
我們本次使用的環境是Ubuntu 16.04 LTS
,實例使用的是阿里雲服務器,首先通過ssh
使用root
賬戶連接到我們的instance
:
(注意:由於我們使用的是root
連接,以下命令可以不使用sudo
,如果你遇到了權限問題,請先嚐試在命令前加上sudo
。)
$ ssh root@my-aliyun-server-public-ip-address
連接成功後,我們就可以開始安裝環境了。
首先我們先來安裝PHP 7.1
。介於官方的repository
還沒有更新7.1
,我們需要添加一個知名的repository
再進行安裝:
$ apt install software-properties-common (阿里雲默認沒有裝`add-apt-repository`,所以需要先運行該命令)
$ add-apt-repository ppa:ondrej/php
完成之後,更新一下repository
:
$ apt-get update
然後輸入:
$ apt-get install -y php7.1 php7.1-mysql php7.1-mcrypt php7.1-curl php7.1-mbstring php7.1-zip php7.1-xml
完成後php7.1
就安裝完成了。(對於某些dependencies
可以後續手動安裝)
上面的安裝會一併把apache2
安裝好,所以我們可以直接開始配置apache2
。輸入以下命令,
$ apache2ctl configtest
Output
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message
Syntax OK
如果看到上面的輸出,那麼請輸入:
$ nano /etc/apache2/apache2.conf
在文件的最後一行,加上
ServerName my-aliyun-server-public-ip-address
這裏請把my-aliyun-server-public-ip-address
用我們的公共ip
替換。按Ctrl + X
,然後y
,回車進行保存。
再次輸入
$ apache2ctl configtest
我們會看到,之前的警告已經消失:
Syntax OK
現在我們可以輸入
$ systemctl restart apache2
來重啓一下apache2
。完成後,我們就可以開始配置防火牆了:
$ ufw allow in "Apache Full"
接下來,我們使用瀏覽器訪問:
這裏請用公共ip
替換以上的my-aliyun-server-public-ip-address
。如果出現以下的頁面,那麼就說明配置成功了:
接下來我們需要更新apache
配置,這樣當服務器上的某個文件夾被請求時,apache
默認優先獲取index.php
。
$ nano /etc/apache2/mods-enabled/dir.conf
將以下配置:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
中的index.php
移到最前:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
保存退出後重啓apache2即可:
$ systemctl restart apache2
接下來我們需要安裝MySQL
服務器:
$ apt-get install -y mysql-server
安裝過程中,我們需要根據提示輸入root
用戶的密碼。完成後,我們清除不安全的環境:
$ mysql_secure_installation
根據提示操作完成即可。過程中會被問到是否要安裝VALIDATE PASSWORD PLUGIN
這個插件,如果選是,那麼需要設置用戶密碼強度,以及檢測當前root
用戶密碼強度。這裏就不予贅述。