環(huán)境
- Ubuntu 12.04 LTS
- PHP
- Apache2
- Mysql
Web服務(wù)器 Apache2
Apache2 安裝
SSH 登陸 Linode 主機,輸入下列指令安裝Apache2
sudo apt-get install apache2
Apache2 配置
- 保險起見,拷貝配置文件
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.backup.conf
- 打開配置文件
sudo vim /etc/apache2/apache2.conf
- 編輯配置文件
針對Linode 1GB,應(yīng)該做如下配置,以防流量超標(biāo)
KeepAlive Off
...
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 6
MaxSpareServers 12
MaxClients 80
MaxRequestsPerChild 3000
</IfModule>
- 重新啟動Apache2
sudo service apache2 restart
配置虛擬主機
為了安裝Wordpress,需要虛擬主機,這里新建一個系統(tǒng)賬戶,在其下配置,這里用* name-based virtual hosts to host websites in your home directory *
- Disable the default Apache virtual host by entering the following command
取消默認(rèn)站點
sudo a2dissite default
- 在home目錄下,新建Public目錄,以及其下的example.com目錄(自己的網(wǎng)站域名),以及再其下的public,log,backup子目錄
cd ~
mkdir public
mkdir -p public/example.com/{public,log,backup}
- 增加所有人對 home目錄的讀和執(zhí)行權(quán)限
sudo chmod a+rx ~
- 增加所有人對public目錄以及其中所有文件的讀和執(zhí)行權(quán)限
sudo chmod -R a+rx ~/public
- 新建虛擬主機配置文件
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下的用戶名
- example.com : 你的網(wǎng)站域名
- 建立軟鏈接
在sites-available目錄中,建立sites-enables目錄下example.com.conf的軟鏈接,以激活站點
sudo a2ensite example.com.conf
備注:通過a2dissite和a2ensite,我們可以快速激活/屏蔽站點
- 重啟Apache2
sudo service apache2 restart
數(shù)據(jù)庫 Mysql
安裝 Mysql
以下是安裝步驟:
- MySQL database server安裝
sudo apt-get install mysql-server
- 為了Mysql的root用戶設(shè)置密碼
- 打開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
- 打開配置文件
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ù)庫
- 進(jìn)入Mysql
mysql -u root -p
- 創(chuàng)建數(shù)據(jù)庫
create database exampleDB;
- 為上述數(shù)據(jù)庫創(chuàng)建用戶和密碼
grant all on exampleDB.* to 'example_user' identified by 'password';
- 激活設(shè)置的賬戶
flush privileges;
- 退出
quit
導(dǎo)入數(shù)據(jù)庫
導(dǎo)入數(shù)據(jù)庫文件sql
- 將數(shù)據(jù)庫文件上傳至服務(wù)器,對于Mac, 使用開源軟件 Cyberdark,使用SFTP協(xié)議,公鑰登陸
- 將數(shù)據(jù)庫文件導(dǎo)入數(shù)據(jù)庫
mysql -u username -ppassword database_name < FILE.sql
- p和password之間無空格
- username和password以及database_name都是已經(jīng)建立好的,此語句只是導(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
- 打開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
- 新建目錄用于存儲PHP error log信息
sudo mkdir -p /var/log/php
- 將該目錄的用戶更改為運行PHP的賬號
sudo chown User /var/log/php
- 重新啟動Apache2以加載PHP模塊
sudo service apache2 restart
至此,PHP+Apache+Mysq已經(jīng)安裝完畢
網(wǎng)站的根目錄在** /home/user/public/example.com/public**中
上述文字參考材料:
Linode幫助文檔
Mactalk
剩下的是上傳Wordpress安裝文件并安裝,如果有備份數(shù)據(jù)庫,則還有還原Wordpress的問題
以下,開始正式安裝和恢復(fù)Wordpress
Wordpress
搭建 Wordpress
新建數(shù)據(jù)庫
Mysql中為網(wǎng)站新建數(shù)據(jù)庫lienDB,具體見上文的新建數(shù)據(jù)庫部分
設(shè)定數(shù)據(jù)庫未lienDB,并且用戶名為lien,編碼為utf8
下載WP安裝包并配置
- 先安裝wget
sudo apt-get install wget
- 用wget 語句直接下載安裝包
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ù)庫信息,配置wordpress
cp wp-config-sample.php wp-config.php
vim ~/wordpress/wp-config.php
- 設(shè)置相關(guān)權(quán)限
將當(dāng)前目錄下的所有文件用戶名:用戶組改為 www-data:www-data,并且當(dāng)前用戶添加進(jìn) www-data組
sudo chown www-data:www-data * -R
sudo usermod -a -G www-data username
- 設(shè)置虛擬主機
詳見上文
配置conf文件
sudo vim /etc/apache2/sites-available/example.com.conf
配置好保存后建立軟鏈接
sudo a2ensite example.com.conf
重新啟動Apache2
sudo service apache2 restart
- 若使用Nginx作WEB服務(wù)器,配置見此文
還原Wordpress
有舊Wordpress的數(shù)據(jù)庫文件.sql,將其還原
新建數(shù)據(jù)庫
為該文件新建數(shù)據(jù)庫leDB,并配置好用戶和編碼
導(dǎo)入數(shù)據(jù)庫
- 更改原數(shù)據(jù)庫文件中的域名設(shè)置
因為原備份網(wǎng)站是存放在根目錄的wordpress目錄下,新站直接放在根目錄,并且域名也進(jìn)行了更換,所以數(shù)據(jù)庫文件要做處理。
.sql文件用文本工具打開
- 將www.lienzh.com替換為www.lienzh.me
- 因為原網(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ù)庫文件上傳至主機,并導(dǎo)入leDB
更改Wordpress配置文件wp-config.php
另一方法是,刪除wp-config.php文件(備份好),再打開網(wǎng)址,WP會重新生成wp-config.php文件,前提是,目錄下存在wp-config-sample.php文件虛擬主機不需要重新設(shè)置,重新啟動Apache2即可
Wordpress 后臺配置中遇到的問題
問題:出現(xiàn)在WordPress后臺更改固定鏈接設(shè)置出現(xiàn)404錯誤,具體錯誤提示是:
The requested URL / was not found on this server.
可能原因:
- wordpress的固定鏈接需要apache的rewrite功能支持。
- apache2默認(rèn)沒有打開rewrite功能。
- wordpress的vhost配置沒加入完全的rewrite功能。
- apache2沒有wordpress目錄的寫權(quán)限,不能寫入.htaccess
我的系統(tǒng):
我的系統(tǒng)問題在第四點,所以如下:
sudo a2enmod rewrite
應(yīng)該等同于以下的命令,未嘗試:
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/rewrite.load rewrite.load
重新啟動Apache2即可
sudo service apache2 restart