Centos7_WordPress快速搭建个人博客

一、系统环境

[root@ecs-35f2 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@ecs-35f2 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether fa:16:3e:49:29:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.181/24 brd 192.168.2.255 scope global dynamic eth0
       valid_lft 86260sec preferred_lft 86260sec
    inet6 fe80::f816:3eff:fe49:29b4/64 scope link 
       valid_lft forever preferred_lft forever

为了方便测试,停止并且开机不启动防火墙,生产环境根据需要进行调整

[root@ecs-35f2 ~]# systemctl stop firewalld
[root@ecs-35f2 ~]# systemctl disable firewalld

设置selinux=disabled

[root@ecs-35f2 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

二、yum 安装Apache+PHP+MySQL(LAMP)环境,(或者搜索XAMPP集成包直接安装,这里不演示)

1、安装Apache

yum方式安装

[root@ecs-35f2 ~]# yum install httpd -y

[root@ecs-35f2 ~]# rpm -qa | grep httpd
httpd-manual-2.4.6-67.el7.centos.6.noarch
httpd-tools-2.4.6-67.el7.centos.6.x86_64
httpd-2.4.6-67.el7.centos.6.x86_64

启动httpd并设置开机启动

[root@ecs-35f2 ~]# systemctl start httpd.service 
[root@ecs-35f2 ~]# systemctl enable  httpd.service 

使用浏览器打开本地IP查看

2、安装PHP

yum 方式安装php

[root@ecs-35f2 ~]# yum install php php-mysql -y


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

在Apache网页目录下创建一个.php结尾的php文件并重启httpd服务,测试能否正常访问

[root@ecs-35f2 ~]# vim /var/www/html/test.php
[root@ecs-35f2 ~]# systemctl restart httpd.service 
[root@ecs-35f2 ~]# cat  /var/www/html/test.php
<?php
phpinfo();
?>

使用浏览器打开本地IP查看

3、安装数据库,Centos7更名为mariadb

yum方式安装

[root@ecs-35f2 ~]# yum install mariadb-server -y

[root@ecs-35f2 ~]# rpm -qa | grep mariadb
mariadb-5.5.56-2.el7.x86_64
mariadb-server-5.5.56-2.el7.x86_64
mariadb-libs-5.5.56-2.el7.x86_64

启动并设置开机启动

[root@ecs-35f2 ~]# systemctl start mariadb.service 
[root@ecs-35f2 ~]# systemctl enable  mariadb.service

初始化数据库

[root@ecs-35f2 ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): (输入原始root密码,若无enter)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y(是否设置root密码)
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y(是否移除匿名用户)
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n(是否禁止远程root登录)
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y(是否删除测试数据库)
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y(重新载入)
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

为wordpress创建数据库和用户,注意这里的数值要和等下安装的wordpress里面的wp-config.php一致

[root@ecs-35f2 ~]# mysql -uroot -ppass
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
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)]> create database wpdb character set utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on wpdb.* to 'root'@'localhost' identified by 'pass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

三、安装wordpress,这里使用wget方式下载到本地,下载链接到官网查询:https://cn.wordpress.org/

先yum安装wget,然后下载wordpress安装包

[root@ecs-35f2 ~]# yum install wget -y

[root@ecs-35f2 ~]# wget https://cn.wordpress.org/wordpress-4.9.1-zh_CN.tar.gz

解压缩wordpress安装包并拷贝到所有文件到/var/www/html,添加wp-config.php

[root@ecs-35f2 ~]# tar -xvzf wordpress-4.9.1-zh_CN.tar.gz 
[root@ecs-35f2 ~]# ll
total 9920
-rw-------. 1 root   root      1138 Sep 12 12:24 anaconda-ks.cfg
drwxr-xr-x  5 nobody 65534     4096 Nov 30 20:20 wordpress
-rw-r--r--  1 root   root  10130710 Nov 30 20:20 wordpress-4.9.1-zh_CN.tar.gz
[root@ecs-35f2 ~]# cp -R wordpress/* /var/www/html/

这里修改一下权限(可以忽略,如果等下外网访问网页空白再修改即可)

[root@ecs-35f2 ~]#chown -R roo:root /var/www/html/

复制配置文件

[root@ecs-35f2 html]# cp wp-config-sample.php  wp-config.php

可选,懒得验证,直接改权限

[root@ecs-35f2 ~]#chmod 777 /var/www/html/wp-config.php

修改wp-config.php配置文件,这里的用户名,密码,字符编码都是刚才初始化数据库定义好了

[root@ecs-35f2 html]# vim wp-config.php 

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wpdb');

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

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

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

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

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');

重启数据库和httpd

[root@ecs-35f2 html]# systemctl restart mariadb.service 
[root@ecs-35f2 html]# systemctl restart httpd.service 

内网访问测试



云主机公网IP访问测试,由于80端口需要备案,因此修改httpd为88端口。这里需要注意点是,要先在web页面修改好端口再回到服务器修改httpd的端口

[root@ecs-35f2 html]# vim /etc/httpd/conf/httpd.conf 

Listen 88

要不就会出现,能telnet通端口,但是网页访问异常,如下是内网192.168.2.181映射的公网IP

重启httpd服务

[root@ecs-35f2 html]# systemctl restart httpd.service 

之前遇到一个问题,在内网其他云主机访问服务器内网IP和公网IP都正常,但是在外网机器访问公网IP就页面空白。(这里是某平台云主机测试结果)
内网其他云主机访问内网IP:

内网其他云主机访问公网IP:

外网设备访问服务器公网IP出现图片显示异常:

处理方法是再次配置一次就成功:

1、wp更换别的数据名,其他不用变。

[root@ecs-35f2 html]# vim wp-config.php 
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

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

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

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

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

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');/

2、重新初始化数据并配置数据库,使用原来密码

[root@ecs-35f2 ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): (输入原始root密码,若无enter)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y(是否设置root密码)
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y(是否移除匿名用户)
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n(是否禁止远程root登录)
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y(是否删除测试数据库)
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y(重新载入)
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!



[root@ecs-35f2 ~]# mysql -uroot -ppass
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
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)]> create database wpdb character set utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on wordpress.* to 'root'@'localhost' identified by 'pass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

然后再重启数据和httpd服务,就可以访问了,记住这里我是用修改后的88端口访问,由于虚机被删除了就不贴图演示。完成!