Linux搭建wordpress博客系统

wordpress是基于php+mysql开发的博客系统,使用这套系统,就要搭建php环境,本文以Centos7+LAMP为例,说明如何搭建wordpress博客系统。

一、安装LAMP(Linux+Apache+MySQL+PHP)

当前环境

[root@centos7 ~]# more /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

基于yum方式安装httpd,php,mariadb

[root@centos7 ~]# yum install httpd -y
[root@centos7 ~]# yum install php php-mysql -y
[root@centos7 ~]# yum install mariadb-server -y

验证相关安装包

[root@centos7 ~]# rpm -qa|grep httpd
httpd-tools-2.4.6-67.el7.centos.6.x86_64
httpd-2.4.6-67.el7.centos.6.x86_64

[root@centos7 ~]# rpm -qa|grep php
php-mysql-5.4.16-43.el7_4.x86_64
php-common-5.4.16-43.el7_4.x86_64
php-pdo-5.4.16-43.el7_4.x86_64
php-5.4.16-43.el7_4.x86_64
php-cli-5.4.16-43.el7_4.x86_64

[root@centos7 ~]# rpm -qa|grep maria
mariadb-server-5.5.56-2.el7.x86_64
mariadb-libs-5.5.56-2.el7.x86_64
mariadb-5.5.56-2.el7.x86_64

启动及验证httpd

[root@centos7 ~]# systemctl start httpd
[root@centos7 ~]# ss -nltp|grep httpd
LISTEN     0     128          *:80      *:*          
users:(("httpd",pid=14248,fd=3),("httpd",pid=14207,fd=3),
("httpd",pid=14122,fd=3),("httpd",pid=14112,fd=3),
("httpd",pid=14080,fd=3),("httpd",pid=13907,fd=3),
("httpd",pid=13871,fd=3),("httpd",pid=13775,fd=3),
("httpd",pid=13770,fd=3),("httpd",pid=13678,fd=3),
("httpd",pid=13610,fd=3))

[root@centos7 ~]# echo "httpd test success!">/var/www/html/index.html
[root@centos7 ~]# curl http://localhost
httpd test success!

启动及验证mariadb

[root@centos7 ~]# rpm -ql mariadb-server|grep service
/usr/lib/systemd/system/mariadb.service
[root@centos7 ~]# systemctl start mariadb.service
[root@centos7 ~]# mysql

Welcome to the MariaDB monitor.? Commands end with ; or \g.
Your MariaDB connection id is 1524
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>? show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye

测试PHP

由于本文PHP使用模块化方式装载到httpd,无需单独启动,如下可检测php5模块是否加载

[root@centos7 ~]# httpd -M|grep php
php5_module (shared)
[root@centos7 ~]# echo "<?php echo 'hello world'; ?>" > /var/www/html/index.php
[root@centos7 ~]# curl http://localhost/index.php
hello world

验证PHP连接mariadb数据库

[root@centos7 ~]# vim /var/www/html/connmysql.php
<?php $conn=mysql_connect("localhost","root", ""); if ($conn) echo "succ"; else echo "fail"; mysql_close($conn); ?>
[root@centos7 ~]# curl http://localhost/connmysql.php
succ

二、安装及配置wordpress

[root@centos7 ~]# cd /usr/local/src
[root@centos7 ~]# wget https://cn.wordpress.org/wordpress-4.8.1-zh_CN.tar.gz
[root@centos7 src]# ls
wordpress-4.8.1-zh_CN.tar.gz

[root@centos7 src]# tar -xf wordpress-4.8.1-zh_CN.tar.gz
[root@centos7 src]# cp -R wordpress /var/www/html/

[root@centos7 ~]# cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
[root@centos7 ~]# vim /var/www/html/wordpress/wp-config.php

###主要是配置数据库名及用户密码

/** WordPress数据库的名称 */
define('DB_NAME', 'wpdb');

/** MySQL数据库用户名 */
define('DB_USER', 'wpadmin');

/** MySQL数据库密码 */
define('DB_PASSWORD', 'pass');

/** MySQL主机 */
define('DB_HOST', 'localhost');

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');

创建wordpress数据库及用户

[root@centos7 ~]# mysql
MariaDB [(none)]> create database wpdb character set utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on wpdb.* to 'wpadmin'@'localhost' identified by 'pass';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

配置wordpress
打开浏览器完成wordpress配置,假设你机子ip为xxx.xxx.xxx.xxx,链接如下:
http://xxx.xxx.xxx.xxx/wordpress/wp-admin/install.php

然后,输入网站名称、管理帐号、密码、邮箱,就ok了

三、wordpress ftp更新改成直接更新

使用WordPress安装主题或插件时都要求填写FTP账号和密码,但是有的不能连接成功。有一种解决方法,可替代ftp更新

修改wordpress目录权值和用户组(httpd默认配置用户组apache)

[root@centos7 ~]# cd /var/www/html/wordpress/wp-content/
[root@centos7 wp-content]# mkdir tmp
[root@centos7 wp-content]# chmod 777 tmp
[root@centos7 wp-content]# chmod 777 plugins
[root@centos7 wp-content]# chmod 777 themes
[root@centos7 wp-content]# chown -R apache /var/www/html/wordpress

修改wordpress配置,相对配置如下:

[root@centos7 wp-content]# vim /var/www/html/wordpress/wp-config.php

/** WordPress目录的绝对路径。 */
if ( !defined('ABSPATH') )
    ?define('ABSPATH', dirname(__FILE__) . '/'); 
define('WP_TEMP_DIR', ABSPATH.'wp-content/tmp');
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);

四、wordpress重要插件

代码高亮插件 SyntaxHighlighter Evolved

对比了几款插件WP-Syntax、WP-SynHighlight、Crayon Syntax Highlighter,还是上面那款好用,虽然不支持可视化编辑。当然,仁者见仁智者见智,自己用过才知道好不好。

插件使用方法如下:

http://xxx.xxx.xxx.xxx/wordpress/wp-admin/plugin-install.php

搜索插件 -> 现在安装 -> 启用

3 thoughts on “Linux搭建wordpress博客系统”

发表评论

电子邮件地址不会被公开。 必填项已用*标注