如今有不少的我的博客框架,好比靜態頁面的Jekyll/hexo,PHP語言框架的emlog/zblog,以及本文講到的WordPress。雖然WordPress已是一個10年前誕生的產物,但隨着不斷的版本更新,今天WordPress依然在穩定性/擴展性和易用性上穩拔頭籌。php
如何搭建WordPress的運行環境對於不少人來說倒是一個沒法繞開的問題,下面我就來說一下如何一鍵搭建WordPress博客環境。html
打開命令行輸入下述命令:mysql
wget https://github.com/nfer/wordpress_install_kickstart/raw/master/wordpress_install_kickstart.sh chmod +x wordpress_install_kickstart.sh ./wordpress_install_kickstart.sh
一杯茶(或一杯咖啡)以後,你就能夠體驗WordPress了。git
注:本文討論的方法是在Ubuntu環境下,在阿里雲和本地虛擬機上均測試經過。github
詳細展開,讓咱們看一下這個wordpress_install_kickstart.sh腳本具體作了哪些事情。web
# first we MUST update the apt source apt-get update
第一步,咱們須要先把應用源更新一下,畢竟安裝後續的apache/mysql之類的都須要獲取最新的版本。sql
WordPress是一個服務器端的程序,必需要有一個HTTP Server來進行承載,這裏咱們選用apache做爲HTTP Server。數據庫
# install apache2 apt-get install -y apache2 # test apache2 run # test1: is in background thread IS_APACHE2_IN_BG=`ps xuax | grep -v grep | grep apache2` if [ -z "$IS_APACHE2_IN_BG" ]; then echo "ERROR!!! not found apache2 in background threads"; exit; fi echo "found apache2 in background threads"; #test2: check wget result wget http://localhost/ --spider -q if [ $? -ne 0 ]; then echo "ERROR!!! http://localhost/ not works"; exit; fi echo "http://localhost/ works well";
這一步呢,咱們安裝了Apache,並使用localhost來測試apache是否正常運行。apache
在安裝了Apache以後,一樣咱們須要安裝php,畢竟WordPress框架是一個php語言框架。api
# install php5 and apache php5 mode apt-get install -y libapache2-mod-php5 php5 # test apach2-php5 run echo '' > /var/www/html/phptest.php wget http://localhost/phptest.php -q -O phptest_result.txt PHPTEST_RESULT=`cat phptest_result.txt` rm phptest_result.txt rm /var/www/html/phptest.php if [ ! "$PHPTEST_RESULT" = "hello world" ]; then echo "ERROR!!! php test faild"; exit; fi echo "php test pass";
注意,這裏咱們不只安裝了php5,同時也安裝了apache下的php5組件,這樣纔可使用php5的web模式。
在安裝完成後,咱們一樣使用了localhost測試了php環境是否可以正常輸出。
# install php5-curl apt-get install -y php5-curl
這一步不是必須,可是我在實際運行環境中使用到了smtp插件,其中發送郵件部分就使用到了curl族函數,那麼就必需要按照php5的curl組件。
LAMP,就是LinuxApacheMysqlPhp,現在Linux環境/Apache服務/Php環境都已OK,下一步就是安裝Mysql。
# install mysql silently debconf-set-selections <<< 'mysql-server mysql-server/root_password password root' debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root' apt-get -y install mysql-server mysql-client # test1: is in background thread IS_MYSQLD_IN_BG=`ps xuax | grep -v grep | grep mysqld` if [ -z "$IS_MYSQLD_IN_BG" ]; then echo "ERROR!!! not found mysqld in background threads"; exit; fi echo "found mysqld in background threads"; #test2: check mysql user/password mysql -u root -proot -e '' if [ $? -ne 0 ]; then echo "ERROR!!! mysql user/password error"; exit; fi echo "mysql user/password pass";
注意,這一步我使用了靜態模式安裝,即避免了在安裝過程當中須要手動輸入mysql的管理密碼,一樣在安裝完成後,咱們使用mysql驗證是否運行正常且密碼設置成功。
安裝完mysql後,咱們還須要把mysql做爲php的一個組件,這樣才能夠經過php來調用和操做mysql。
# install php5-mysql apt-get install -y php5-mysql # add mysql extension in apache2/php.ini and restart apache echo "extention=mysql.so" >> /etc/php5/apache2/php.ini
注意,這裏安裝了php5-mysql組件並在php5的web模式配置文件中將mysql組件註冊一下。
# modify the default http root path to /var/www/ and restart apache sed -i 's/html//g' /etc/apache2/sites-enabled/000-default.conf /etc/init.d/apache2 restart
最後,咱們並無直接把WordPress安裝到/var/www/html/,而是把apache的根目錄回退到/var/www/這一級。完成最後這一步,LAMP的環境就OK了,這個時候咱們把apache重啓一下,讓全部的設置所有生效。
#download wordpress the last release archive wget https://wordpress.org/latest.zip # install unzip tools and unzip the archive file apt-get -y install unzip unzip latest.zip rm latest.zip # move wordpress to the http server path mv wordpress /var/www/
首先咱們須要下載並解壓最新版本的WordPress並放置到/var/www/目錄。
咱們須要手動建立一下數據庫:
mysql -u root -proot -e 'CREATE DATABASE IF NOT EXISTS wordpress DEFAULT CHARSET utf8 COLLATE utf8_general_ci;'
下一步就是把數據庫配置寫入配置文件中:
echo "define('DB_NAME', 'wordpress'); define('DB_PASSWORD', 'root'); define('DB_HOST', 'localhost'); define('DB_CHARSET', 'utf8mb4'); define('DB_COLLATE', ''); " > /var/www/wordpress/wp-config.php
WordPress用了一組隨機數來做爲系統內部判斷登錄/鑑權等使用,具體須要查看WordPress相關資料。
wget https://api.wordpress.org/secret-key/1.1/salt/ -O salt.txt -q cat salt.txt >> /var/www/wordpress/wp-config.php rm salt.txt
最後就是一個數據庫表名字前綴,默認都是wp_,
echo " \$table_prefix = 'wp_'; " >> /var/www/wordpress/wp-config.php
到目前爲止,WordPress的安裝和配置就OK了,下一步就是著名WordPress的5分鐘安裝(其實是配置)。
在安裝完WordPress後須要進行的一些配置和操做詳見個人其餘文章:
安裝WordPress後的必備設置和修改2-解決google字體沒法訪問的問題
安裝WordPress後的必備設置和修改3-關閉系統更新監測
安裝WordPress後的必備設置和修改4-解決發送郵件失敗的問題
本文同步發表於:NferZhuang我的網站,CSDN博客,開源中國博客