1. 程式人生 > >一鍵搭建WordPress部落格環境(OneStep to WordPress)

一鍵搭建WordPress部落格環境(OneStep to WordPress)

WordPress介紹


現在有很多的個人部落格框架,比如靜態頁面的Jekyll/hexo,PHP語言框架的emlog/zblog,以及本文講到的WordPress。雖然WordPress已經是一個10年前誕生的產物,但隨著不斷的版本更新,今天WordPress依然在穩定性/擴充套件性和易用性上穩拔頭籌。

如何搭建WordPress的執行環境對於很多人來講卻是一個無法繞開的問題,下面我就來講一下如何一鍵搭建WordPress部落格環境。

開啟命令列輸入下述命令:

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了。

注:本文討論的方法是在Ubuntu環境下,在阿里雲和本地虛擬機器上均測試通過。

安裝LAMP環境

詳細展開,讓我們看一下這個wordpress_install_kickstart.sh指令碼具體做了哪些事情。

# first we MUST update the apt source
apt-get update 
第一步,我們需要先把應用源更新一下,畢竟安裝後續的apache/mysql之類的都需要獲取最新的版本。

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之後,同樣我們需要安裝php,畢竟WordPress框架是一個php語言框架。

# 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重啟一下,讓所有的設定全部生效。

安裝WordPress

#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後需要進行的一些配置和操作詳見我的其他文章: