Ubuntu+PHP+Apache+Mysql搭建Wordpress以及恢復(fù)備份sql

環(huán)境

  • Ubuntu 12.04 LTS
  • PHP
  • Apache2
  • Mysql

Web服務(wù)器 Apache2

Apache2 安裝

SSH 登陸 Linode 主機,輸入下列指令安裝Apache2

sudo apt-get install apache2
Apache2 配置
  1. 保險起見,拷貝配置文件
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.backup.conf
  1. 打開配置文件
sudo vim /etc/apache2/apache2.conf
  1. 編輯配置文件
    針對Linode 1GB,應(yīng)該做如下配置,以防流量超標(biāo)
KeepAlive Off
...
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 6
MaxSpareServers 12
MaxClients 80
MaxRequestsPerChild 3000
</IfModule>
  1. 重新啟動Apache2
sudo service apache2 restart
配置虛擬主機

為了安裝Wordpress,需要虛擬主機,這里新建一個系統(tǒng)賬戶,在其下配置,這里用* name-based virtual hosts to host websites in your home directory *

  1. Disable the default Apache virtual host by entering the following command
    取消默認(rèn)站點
sudo a2dissite default
  1. 在home目錄下,新建Public目錄,以及其下的example.com目錄(自己的網(wǎng)站域名),以及再其下的public,log,backup子目錄
cd ~
mkdir public
mkdir -p public/example.com/{public,log,backup}
  1. 增加所有人對 home目錄的讀和執(zhí)行權(quán)限
sudo chmod a+rx ~
  1. 增加所有人對public目錄以及其中所有文件的讀和執(zhí)行權(quán)限
sudo chmod -R a+rx ~/public
  1. 新建虛擬主機配置文件
sudo vim /etc/apache2/sites-available/example.com.conf
  1. 作如下配置
# 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)站域名
  1. 建立軟鏈接
    在sites-available目錄中,建立sites-enables目錄下example.com.conf的軟鏈接,以激活站點
sudo a2ensite example.com.conf

備注:通過a2dissite和a2ensite,我們可以快速激活/屏蔽站點

  1. 重啟Apache2
sudo service apache2 restart

數(shù)據(jù)庫 Mysql

安裝 Mysql

以下是安裝步驟:

  1. 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
  1. 打開配置文件
sudo nano /etc/mysql/my.cnf
  1. 作如下設(shè)置
max_connections = 75
key_buffer = 32M
max_allowed_packet = 1M
thread_stack = 128K
table_cache = 32
  1. 重啟Mysql
sudo service mysql restart
創(chuàng)建數(shù)據(jù)庫
  1. 進(jìn)入Mysql
mysql -u root -p
  1. 創(chuàng)建數(shù)據(jù)庫
create database exampleDB;
  1. 為上述數(shù)據(jù)庫創(chuàng)建用戶和密碼
grant all on exampleDB.* to 'example_user' identified by 'password';
  1. 激活設(shè)置的賬戶
flush privileges;
  1. 退出
quit
導(dǎo)入數(shù)據(jù)庫

導(dǎo)入數(shù)據(jù)庫文件sql

  1. 將數(shù)據(jù)庫文件上傳至服務(wù)器,對于Mac, 使用開源軟件 Cyberdark,使用SFTP協(xié)議,公鑰登陸
  2. 將數(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

以下是安裝步驟

  1. Install the base PHP package by entering the following command:
sudo apt-get install php5 php-pear
  1. Add MySQL support by entering the following command:
sudo apt-get install php5-mysql
  1. Secure PHP with Suhosin by entering the following command:
sudo apt-get install php5-suhosin
Optimizing PHP for a Linode 1GB
  1. 打開PHP配置文件
sudo vim /etc/php5/apache2/php.ini
  1. 作如下配置
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

  1. 新建目錄用于存儲PHP error log信息
sudo mkdir -p /var/log/php
  1. 將該目錄的用戶更改為運行PHP的賬號
sudo chown User /var/log/php
  1. 重新啟動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安裝包并配置
  1. 先安裝wget
sudo apt-get install wget
  1. 用wget 語句直接下載安裝包
wget https://wordpress.org/latest.tar.gz
  1. 解壓該文件
tar -xzvf latest.tar.gz
  1. 將解壓的文件放在根目錄* /home/user/public/example.com/public*下
sudo cp -r ~/wordpress/* ~/public/example.com/public/
  1. 根據(jù)數(shù)據(jù)庫信息,配置wordpress
cp wp-config-sample.php wp-config.php
vim ~/wordpress/wp-config.php
  1. 設(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
  1. 設(shè)置虛擬主機
    詳見上文
    配置conf文件
sudo vim /etc/apache2/sites-available/example.com.conf

配置好保存后建立軟鏈接

sudo a2ensite example.com.conf

重新啟動Apache2

sudo service apache2 restart
  1. 若使用Nginx作WEB服務(wù)器,配置見此文
還原Wordpress

有舊Wordpress的數(shù)據(jù)庫文件.sql,將其還原

新建數(shù)據(jù)庫

為該文件新建數(shù)據(jù)庫leDB,并配置好用戶和編碼

導(dǎo)入數(shù)據(jù)庫
  1. 更改原數(shù)據(jù)庫文件中的域名設(shè)置
    因為原備份網(wǎng)站是存放在根目錄的wordpress目錄下,新站直接放在根目錄,并且域名也進(jìn)行了更換,所以數(shù)據(jù)庫文件要做處理。
    .sql文件用文本工具打開
 UPDATE wp_options SET option_value = 'http://www.lienzh.me' WHERE option_id = 2;
  1. 將修改后的數(shù)據(jù)庫文件上傳至主機,并導(dǎo)入leDB

  2. 更改Wordpress配置文件wp-config.php
    另一方法是,刪除wp-config.php文件(備份好),再打開網(wǎng)址,WP會重新生成wp-config.php文件,前提是,目錄下存在wp-config-sample.php文件

  3. 虛擬主機不需要重新設(shè)置,重新啟動Apache2即可

Wordpress 后臺配置中遇到的問題

問題:出現(xiàn)在WordPress后臺更改固定鏈接設(shè)置出現(xiàn)404錯誤,具體錯誤提示是:

The requested URL / was not found on this server.

可能原因

  1. wordpress的固定鏈接需要apache的rewrite功能支持。
  2. apache2默認(rèn)沒有打開rewrite功能。
  3. wordpress的vhost配置沒加入完全的rewrite功能。
  4. 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

此問題有用的參考材料

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • //用tasksel可以一鍵安裝lamp的集合環(huán)境包 root一、安裝(以root的身份登入系統(tǒng),不需要輸入前邊的...
    Tangbh閱讀 1,684評論 1 2
  • 引言:因為需要,最近打算為我們的CTF戰(zhàn)隊搭建一個網(wǎng)站,因此買了一個國外的VPS,操作系統(tǒng)是ubuntu的,準(zhǔn)備用...
    jessica1123閱讀 9,211評論 4 8
  • 前期準(zhǔn)備 wordpress安裝包,官網(wǎng)鏈接 1. 安裝apache2.0 sudo apt-get instal...
    macfish閱讀 1,293評論 0 3
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,915評論 18 139
  • 清舟的世界安靜了,他逃離了爭吵不斷的父母,不再回來。 2017年,7月,仲夏之末,晴朗。 ...
    寶澤夫閱讀 348評論 1 1