WordPress简介
WordPress是一种使用PHP和MySQL开发的博客平台,其实对个人来说也可以当做一个像印象笔记一样的个人知识、内容管理平台。同时各种第三方的插件、主题也让WordPress具备了独特的个性化选择。
手动安装WordPress
安装WordPress相对来说是一个比较简单的过程,只要按照以下步骤执行,一般都可以顺利安装。
1.安装依赖的软件包
第一步要在Ubuntu上安装WordPress所依赖的MySQL、Apache2、PHP等软件包,具体安装方法如下:
sudo apt-get install apache2
sudo apt-get install libapache2-mod-php5 php5
sudo apt-get install mysql-server-5.0 mysql-common mysql-admin
sudo apt-get install php5-mysql
2.配置MySQL
在上一步中安装完MySQL之后记住MySQL的root用户的密码,在这一步中将会使用。使用如下命令来进入MySQL数据库:
mysql -u root -p
然后输入密码进入MySQL。接着我们创建WordPress所使用的数据库:
create database wordpress;
接着为WordPress创建用户并将数据库wordpress的所有权限赋予这个用户:
create user wpuser@localhost identified by 'wppasswd';
grant all privileges on wordpress.* to wpuser@localhost;
flush privileges;
3.下载并安装WordPress
WordPress的安装文件既可以访问中文官方网站进行下载,也可以使用wget命令下载:
wget -c http://wordpress.org/latest.tar.gz
下载完成后解压文件,复制 wp-config-sample.php 文件并重命名为 wp-config.php,并将相关参数进行替换,将
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
/** MySQL hostname */
define('DB_HOST', 'localhost');
修改为:
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wpuser');
/** MySQL database password */
define('DB_PASSWORD', 'wppasswd');
/** MySQL hostname */
define('DB_HOST', 'localhost');
其中’DB_HOST’因为MySQL就在本地所以就不用修改localhost,如果数据库位于其他服务器那么就应该填写相应域名或者IP地址。
4.拷贝WordPress解压文件至Apache2服务器目录下
我们要将解压出来的wordpress文件夹拷贝至/var/www/html/下面,为其创建uploads文件夹(保存上传的图片)并修改文件的拥有者为www-data。
sudo rsync -avP ./wordpress /var/www/html/
sudo mkdir /var/www/html/wordpress/wp-content/uploads
sudo chown -R www-data:www-data /var/www/html/wordpress/*
5.安装MySQL扩展
如果遇到“ Your PHP installation appears to be missing the MySQL extension which is required by WordPress ”的情况,一般在 /etc/php5/apache2/php.ini 中将“ extension=/path/to/extension/mysql.so ”改为“ extension=mysql.so ”就可以了。
6.修改apache2的配置文件来允许URL的重写
本步骤的目的是为了允许WordPress能够自定义固定链接的格式。在/etc/apache2/sites-available/000-default.conf中添加下文星号包括的部分:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
*ServerName 127.0.0.1*
*<Directory /var/www/html/>*
*AllowOverride All*
*</Directory>*
. . .
然后使用如下命令来允许URL的重写以及Apache2的重启:
sudo a2enmod rewrite
sudo service apache2 restart
最后创建 .htaccess 文件并修改其权限与所有者:
touch /var/www/html/wordpress/.htaccess
sudo chown :www-data /var/www/html/wordpress/.htaccess
chmod 664 /var/www/html/wordpress/.htaccess
7.著名的5分钟安装
打开浏览器登录 http://localhost/wordpress 按照图形界面的提示操作即可。
Shell脚本
以下的脚本本人亲测,使用了expect命令来简化设置过程,并添加了几个步骤来修正可能出现的问题.
#!/bin/sh
if [ $# -ne 4 ]; then
echo "Usage: sudo sh $0 db_name user_name user_passwd mysql_root_passwd"
exit 1
fi
db_name=$1
user_name=$2
user_passwd=$3
mysql_root_passwd=$4
install_dependency(){
#1. install the dependency of wordpress
sudo apt-get install apache2
sudo apt-get install libapache2-mod-php5 php5
sudo apt-get install mysql-server mysql-common
sudo apt-get install php5-mysql
}
config_mysql(){
#2. configure mysql
sudo apt-get install expect
expect << EOF
set timeout 100
spawn mysql -u root -p
expect {
"Enter password:" {send "$mysql_root_passwd\r"}
}
expect "mysql>"
send "create database $db_name;\r"
expect "mysql>"
send "create user $user_name@localhost identified by '$user_passwd';\r"
expect "mysql>"
send "grant all privileges on $db_name.* to $user_name@localhost;\r"
expect "mysql>"
send "flush privileges;\r"
expect "mysql>"
send "exit\r"
EOF
}
install_wordpress(){
#3. install wordpress
#3.1 download the wordpress
wget -c http://wordpress.org/latest.tar.gz
#3.2 extract the files to rebuild wordpress
tar xvfz latest.tar.gz
sudo rm -f latest.tar.gz
#3.3 install some packages to allow you to work with images, install plugins and update portions of your site using ssh.
sudo apt-get install php5-gd libssh2-php
}
config_wordpress(){
#4. configure wordpress
sed "s/database_name_here/$db_name/" ./wordpress/wp-config-sample.php | sed "s/username_here/$user_name/" | sed "s/password_here/$user_passwd/" >> ./wordpress/wp-config.php
}
copy_files(){
#5. copy files to the document root
sudo rsync -avP ./wordpress /var/www/html/
sudo rm -rf ./wordpress
sudo mkdir /var/www/html/wordpress/wp-content/uploads
sudo chown -R www-data:www-data /var/www/html/wordpress/*
}
install_mysql_extension(){
#6. when the problem "Your PHP installation appears to be missing the MySQL extension which is required by WordPress" occurs, you need this.
sudo sed -i "s/extension=\/path\/to\/extension\/msql.so/extension=mysql.so/" /etc/php5/apache2/php.ini
}
modify_apache_to_allow_url_rewrites(){
date_time=$(date +%y%m%d%H%M)
sudo cp /etc/apache2/sites-available/000-default.conf "/etc/apache2/sites-available/000-default.conf.$date_time.bak"
sudo sed -i "/<\/VirtualHost>/i \ ServerName 127.0.0.1\n <Directory \/var\/www\/html\/wordpress\/>\n AllowOverride All\n <\/Directory>" /etc/apache2/sites-available/000-default.conf
sudo a2enmod rewrite
sudo echo 'ServerName localhost' >> /etc/apache2/apache2.conf
sudo service apache2 restart
}
create_htaccess_file(){
touch /var/www/html/wordpress/.htaccess
sudo chown :www-data /var/www/html/wordpress/.htaccess
chmod 664 /var/www/html/wordpress/.htaccess
}
#install_dependency
config_mysql
install_wordpress
config_wordpress
copy_files
install_mysql_extension
modify_apache_to_allow_url_rewrites
create_htaccess_file
exit 0
“在Ubuntu上安装WordPress(附脚本)”的一个回复