美文网首页程序员ubuntu我爱编程
Ubuntu+PHP+Apache+Mysql搭建Wordpre

Ubuntu+PHP+Apache+Mysql搭建Wordpre

作者: LienZzzz | 来源:发表于2014-07-07 02:18 被阅读1319次

环境

  • Ubuntu 12.04 LTS
  • PHP
  • Apache2
  • Mysql

Web服务器 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,应该做如下配置,以防流量超标
KeepAlive Off
...
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 6
MaxSpareServers 12
MaxClients 80
MaxRequestsPerChild 3000
</IfModule>
  1. 重新启动Apache2
sudo service apache2 restart
配置虚拟主机

为了安装Wordpress,需要虚拟主机,这里新建一个系统账户,在其下配置,这里用* name-based virtual hosts to host websites in your home directory *

  1. Disable the default Apache virtual host by entering the following command
    取消默认站点
sudo a2dissite default
  1. 在home目录下,新建Public目录,以及其下的example.com目录(自己的网站域名),以及再其下的public,log,backup子目录
cd ~
mkdir public
mkdir -p public/example.com/{public,log,backup}
  1. 增加所有人对 home目录的读和执行权限
sudo chmod a+rx ~
  1. 增加所有人对public目录以及其中所有文件的读和执行权限
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 : 你的网站域名
  1. 建立软链接
    在sites-available目录中,建立sites-enables目录下example.com.conf的软链接,以激活站点
sudo a2ensite example.com.conf

备注:通过a2dissite和a2ensite,我们可以快速激活/屏蔽站点

  1. 重启Apache2
sudo service apache2 restart

数据库 Mysql

安装 Mysql

以下是安装步骤:

  1. MySQL database server安装
sudo apt-get install mysql-server
  • 为了Mysql的root用户设置密码
  • 打开mysql_secure_installation应用
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. 作如下设置
max_connections = 75
key_buffer = 32M
max_allowed_packet = 1M
thread_stack = 128K
table_cache = 32
  1. 重启Mysql
sudo service mysql restart
创建数据库
  1. 进入Mysql
mysql -u root -p
  1. 创建数据库
create database exampleDB;
  1. 为上述数据库创建用户和密码
grant all on exampleDB.* to 'example_user' identified by 'password';
  1. 激活设置的账户
flush privileges;
  1. 退出
quit
导入数据库

导入数据库文件sql

  1. 将数据库文件上传至服务器,对于Mac, 使用开源软件 Cyberdark,使用SFTP协议,公钥登陆
  2. 将数据库文件导入数据库
mysql -u username -ppassword database_name < FILE.sql
  • p和password之间无空格
  • username和password以及database_name都是已经建立好的,此语句只是导入.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已经安装完毕
网站的根目录在** /home/user/public/example.com/public**中

上述文字参考材料:
Linode帮助文档
Mactalk


剩下的是上传Wordpress安装文件并安装,如果有备份数据库,则还有还原Wordpress的问题
以下,开始正式安装和恢复Wordpress

Wordpress

搭建 Wordpress
新建数据库

Mysql中为网站新建数据库lienDB,具体见上文的新建数据库部分

设定数据库未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. 根据数据库信息,配置wordpress
cp wp-config-sample.php wp-config.php
vim ~/wordpress/wp-config.php
  1. 设置相关权限
    将当前目录下的所有文件用户名:用户组改为 www-data:www-data,并且当前用户添加进 www-data组
sudo chown www-data:www-data * -R 
sudo usermod -a -G www-data username
  1. 设置虚拟主机
    详见上文
    配置conf文件
sudo vim /etc/apache2/sites-available/example.com.conf

配置好保存后建立软链接

sudo a2ensite example.com.conf

重新启动Apache2

sudo service apache2 restart
  1. 若使用Nginx作WEB服务器,配置见此文
还原Wordpress

有旧Wordpress的数据库文件.sql,将其还原

新建数据库

为该文件新建数据库leDB,并配置好用户和编码

导入数据库
  1. 更改原数据库文件中的域名设置
    因为原备份网站是存放在根目录的wordpress目录下,新站直接放在根目录,并且域名也进行了更换,所以数据库文件要做处理。
    .sql文件用文本工具打开
 UPDATE wp_options SET option_value = 'http://www.lienzh.me' WHERE option_id = 2;
  1. 将修改后的数据库文件上传至主机,并导入leDB

  2. 更改Wordpress配置文件wp-config.php
    另一方法是,删除wp-config.php文件(备份好),再打开网址,WP会重新生成wp-config.php文件,前提是,目录下存在wp-config-sample.php文件

  3. 虚拟主机不需要重新设置,重新启动Apache2即可

Wordpress 后台配置中遇到的问题

问题:出现在WordPress后台更改固定链接设置出现404错误,具体错误提示是:

The requested URL / was not found on this server.

可能原因

  1. wordpress的固定链接需要apache的rewrite功能支持。
  2. apache2默认没有打开rewrite功能。
  3. wordpress的vhost配置没加入完全的rewrite功能。
  4. apache2没有wordpress目录的写权限,不能写入.htaccess

我的系统
我的系统问题在第四点,所以如下:

sudo a2enmod rewrite 

应该等同于以下的命令,未尝试:

cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/rewrite.load rewrite.load

重新启动Apache2即可

sudo service apache2 restart

此问题有用的参考材料

相关文章

网友评论

    本文标题:Ubuntu+PHP+Apache+Mysql搭建Wordpre

    本文链接:https://www.haomeiwen.com/subject/ouijtttx.html