一鍵搭建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的安裝和配置就OK了,下一步就是著名WordPress的5分鐘安裝(實際上是配置)

最後

在安裝完WordPress後需要進行的一些配置和操作詳見我的其他文章:

安裝WordPress後的必備設置和修改

安裝WordPress後的必備設置和修改2-解決google字體無法訪問的問題

安裝WordPress後的必備設置和修改3-關閉系統更新監測

安裝WordPress後的必備設置和修改4-解決發送郵件失敗的問題


本文同步發表於:NferZhuang個人網站CSDN博客開源中國博客

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