環(huán)境
- Ubuntu 12.04 LTS
- PHP
- Apache2
- Mysql
Web服務(wù)器 Apache2
Apache2 安裝
SSH 登陸 Linode 主機(jī),輸入下列指令安裝Apache2
sudo apt-get install apache2
Apache2 配置
- 保險(xiǎn)起見(jiàn),拷貝配置文件
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.backup.conf
- 打開(kāi)配置文件
sudo vim /etc/apache2/apache2.conf
- 編輯配置文件
針對(duì)Linode 1GB,應(yīng)該做如下配置,以防流量超標(biāo)
KeepAlive Off
...
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 6
MaxSpareServers 12
MaxClients 80
MaxRequestsPerChild 3000
</IfModule>
- 重新啟動(dòng)Apache2
sudo service apache2 restart
配置虛擬主機(jī)
為了安裝Wordpress,需要虛擬主機(jī),這里新建一個(gè)系統(tǒng)賬戶(hù),在其下配置,這里用* name-based virtual hosts to host websites in your home directory *
- Disable the default Apache virtual host by entering the following command
取消默認(rèn)站點(diǎn)
sudo a2dissite default
- 在home目錄下,新建Public目錄,以及其下的example.com目錄(自己的網(wǎng)站域名),以及再其下的public,log,backup子目錄
cd ~
mkdir public
mkdir -p public/example.com/{public,log,backup}
- 增加所有人對(duì) home目錄的讀和執(zhí)行權(quán)限
sudo chmod a+rx ~
- 增加所有人對(duì)public目錄以及其中所有文件的讀和執(zhí)行權(quán)限
sudo chmod -R a+rx ~/public
- 新建虛擬主機(jī)配置文件
sudo vim /etc/apache2/sites-available/example.com.conf
- 作如下配置
# domain: example.com
# public: /home/example_user/public/example.com/
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /home/example_user/public/example.com/public
# Log file locations
LogLevel warn
ErrorLog /home/example_user/public/example.com/log/error.log
CustomLog /home/example_user/public/example.com/log/access.log combined
</VirtualHost>
- ServerAdmin webmaster@example.com :管理員郵箱
- example_user : 該Home下的用戶(hù)名
- example.com : 你的網(wǎng)站域名
- 建立軟鏈接
在sites-available目錄中,建立sites-enables目錄下example.com.conf的軟鏈接,以激活站點(diǎn)
sudo a2ensite example.com.conf
備注:通過(guò)a2dissite和a2ensite,我們可以快速激活/屏蔽站點(diǎn)
- 重啟Apache2
sudo service apache2 restart
數(shù)據(jù)庫(kù) Mysql
安裝 Mysql
以下是安裝步驟:
- MySQL database server安裝
sudo apt-get install mysql-server
- 為了Mysql的root用戶(hù)設(shè)置密碼
- 打開(kāi)mysql_secure_installation應(yīng)用
sudo mysql_secure_installation
- Follow the instructions to remove anonymous user accounts, disable remote root login, and remove the test database.
配置 MySQL for a Linode 1GB
- 打開(kāi)配置文件
sudo nano /etc/mysql/my.cnf
- 作如下設(shè)置
max_connections = 75
key_buffer = 32M
max_allowed_packet = 1M
thread_stack = 128K
table_cache = 32
- 重啟Mysql
sudo service mysql restart
創(chuàng)建數(shù)據(jù)庫(kù)
- 進(jìn)入Mysql
mysql -u root -p
- 創(chuàng)建數(shù)據(jù)庫(kù)
create database exampleDB;
- 為上述數(shù)據(jù)庫(kù)創(chuàng)建用戶(hù)和密碼
grant all on exampleDB.* to 'example_user' identified by 'password';
- 激活設(shè)置的賬戶(hù)
flush privileges;
- 退出
quit
導(dǎo)入數(shù)據(jù)庫(kù)
導(dǎo)入數(shù)據(jù)庫(kù)文件sql
- 將數(shù)據(jù)庫(kù)文件上傳至服務(wù)器,對(duì)于Mac, 使用開(kāi)源軟件 Cyberdark,使用SFTP協(xié)議,公鑰登陸
- 將數(shù)據(jù)庫(kù)文件導(dǎo)入數(shù)據(jù)庫(kù)
mysql -u username -ppassword database_name < FILE.sql
- p和password之間無(wú)空格
- username和password以及database_name都是已經(jīng)建立好的,此語(yǔ)句只是導(dǎo)入.sql文件
PHP
PHP is a general-purpose scripting language that allows you to produce dynamic and interactive webpages. Many popular web applications and content management systems, like WordPress and Drupal, are written in PHP. To develop or host websites using PHP, you must first install the base package and a couple of modules
安裝PHP
以下是安裝步驟
- Install the base PHP package by entering the following command:
sudo apt-get install php5 php-pear
- Add MySQL support by entering the following command:
sudo apt-get install php5-mysql
- Secure PHP with Suhosin by entering the following command:
sudo apt-get install php5-suhosin
Optimizing PHP for a Linode 1GB
- 打開(kāi)PHP配置文件
sudo vim /etc/php5/apache2/php.ini
- 作如下配置
max_execution_time = 30
memory_limit = 128M
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
display_errors = Off
log_errors = On
error_log = /var/log/php/error.log
register_globals = Off
注意:
The 128M setting for memory_limit is a general guideline. While this value should be sufficient for most websites, larger websites and some web applications may require 256 megabytes or more
- 新建目錄用于存儲(chǔ)PHP error log信息
sudo mkdir -p /var/log/php
- 將該目錄的用戶(hù)更改為運(yùn)行PHP的賬號(hào)
sudo chown User /var/log/php
- 重新啟動(dòng)Apache2以加載PHP模塊
sudo service apache2 restart
至此,PHP+Apache+Mysq已經(jīng)安裝完畢
網(wǎng)站的根目錄在** /home/user/public/example.com/public**中
上述文字參考材料:
Linode幫助文檔
Mactalk
剩下的是上傳Wordpress安裝文件并安裝,如果有備份數(shù)據(jù)庫(kù),則還有還原Wordpress的問(wèn)題
以下,開(kāi)始正式安裝和恢復(fù)Wordpress
Wordpress
搭建 Wordpress
新建數(shù)據(jù)庫(kù)
Mysql中為網(wǎng)站新建數(shù)據(jù)庫(kù)lienDB,具體見(jiàn)上文的新建數(shù)據(jù)庫(kù)部分
設(shè)定數(shù)據(jù)庫(kù)未lienDB,并且用戶(hù)名為lien,編碼為utf8
下載WP安裝包并配置
- 先安裝wget
sudo apt-get install wget
- 用wget 語(yǔ)句直接下載安裝包
wget https://wordpress.org/latest.tar.gz
- 解壓該文件
tar -xzvf latest.tar.gz
- 將解壓的文件放在根目錄* /home/user/public/example.com/public*下
sudo cp -r ~/wordpress/* ~/public/example.com/public/
- 根據(jù)數(shù)據(jù)庫(kù)信息,配置wordpress
cp wp-config-sample.php wp-config.php
vim ~/wordpress/wp-config.php
- 設(shè)置相關(guān)權(quán)限
將當(dāng)前目錄下的所有文件用戶(hù)名:用戶(hù)組改為 www-data:www-data,并且當(dāng)前用戶(hù)添加進(jìn) www-data組
sudo chown www-data:www-data * -R
sudo usermod -a -G www-data username
- 設(shè)置虛擬主機(jī)
詳見(jiàn)上文
配置conf文件
sudo vim /etc/apache2/sites-available/example.com.conf
配置好保存后建立軟鏈接
sudo a2ensite example.com.conf
重新啟動(dòng)Apache2
sudo service apache2 restart
- 若使用Nginx作WEB服務(wù)器,配置見(jiàn)此文
還原Wordpress
有舊Wordpress的數(shù)據(jù)庫(kù)文件.sql,將其還原
新建數(shù)據(jù)庫(kù)
為該文件新建數(shù)據(jù)庫(kù)leDB,并配置好用戶(hù)和編碼
導(dǎo)入數(shù)據(jù)庫(kù)
- 更改原數(shù)據(jù)庫(kù)文件中的域名設(shè)置
因?yàn)樵瓊浞菥W(wǎng)站是存放在根目錄的wordpress目錄下,新站直接放在根目錄,并且域名也進(jìn)行了更換,所以數(shù)據(jù)庫(kù)文件要做處理。
.sql文件用文本工具打開(kāi)
- 將www.lienzh.com替換為www.lienzh.me
- 因?yàn)樵W(wǎng)站wordpress文件在根目錄的wordpress子目錄下,所以需要修改wp_option數(shù)據(jù)表'siteurl'記錄中的option_value字段,
- 從http://www.lienzh.com/wordpress 更改為 http://www.lienzh.me
- 我在做完第一步后忘了做第二步,所以在命令行中完成
UPDATE wp_options SET option_value = 'http://www.lienzh.me' WHERE option_id = 2;
將修改后的數(shù)據(jù)庫(kù)文件上傳至主機(jī),并導(dǎo)入leDB
更改Wordpress配置文件wp-config.php
另一方法是,刪除wp-config.php文件(備份好),再打開(kāi)網(wǎng)址,WP會(huì)重新生成wp-config.php文件,前提是,目錄下存在wp-config-sample.php文件虛擬主機(jī)不需要重新設(shè)置,重新啟動(dòng)Apache2即可
Wordpress 后臺(tái)配置中遇到的問(wèn)題
問(wèn)題:出現(xiàn)在WordPress后臺(tái)更改固定鏈接設(shè)置出現(xiàn)404錯(cuò)誤,具體錯(cuò)誤提示是:
The requested URL / was not found on this server.
可能原因:
- wordpress的固定鏈接需要apache的rewrite功能支持。
- apache2默認(rèn)沒(méi)有打開(kāi)rewrite功能。
- wordpress的vhost配置沒(méi)加入完全的rewrite功能。
- apache2沒(méi)有wordpress目錄的寫(xiě)權(quán)限,不能寫(xiě)入.htaccess
我的系統(tǒng):
我的系統(tǒng)問(wèn)題在第四點(diǎn),所以如下:
sudo a2enmod rewrite
應(yīng)該等同于以下的命令,未嘗試:
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/rewrite.load rewrite.load
重新啟動(dòng)Apache2即可
sudo service apache2 restart