在CentOS7上搭建WordPress个人博客

在CentOS7上搭建WordPress个人博客

Author:LitMonkey

Time:2019.7.7
Location:NUAA

文章目录

  • 在CentOS7上搭建WordPress个人博客
      • Author:LitMonkey
        • Time:2019.7.7
        • Location:NUAA
    • LAMP
      • 安装Apache
      • 安装PHP7
      • 安装数据库
    • 安装WordPress
    • 总结

LAMP

安装Apache

首先安装Apache(安装httpd):

# root @ VM_0_4_centos in ~ [16:02:01]
$ sudo yum install httpd

启动httpd同时设置为开机启动:

# root @ VM_0_4_centos in ~ [16:04:13]
$ systemctl start httpd.service

# root @ VM_0_4_centos in ~ [16:07:03]
$ systemctl enable  httpd.service

此时打开本地IP验证是否成功打开:

在CentOS7上搭建WordPress个人博客_第1张图片

安装PHP7

接下来安装php7.2。

首先安装EPEL软件包:

$ sudo yum install epel-release

然后安装remi源:

$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

然后安装yum扩展包:

$ sudo yum install yum-utils

最后启用remi仓库:

$ sudo yum-config-manager --enable remi-php72
$ sudo yum update

之后便可以安装php及其他一些模块了:

$ sudo yum install php72 -y
$ sudo yum install php-mysql -y

然后在Apache网页目录下创建一个以.php结尾的php文件并重启httpd服务,

# root @ VM_0_4_centos in ~ [16:20:58]
$ vim  /var/www/html/test.php

# root @ VM_0_4_centos in ~ [16:21:48]
$ cat  /var/www/html/test.php


# root @ VM_0_4_centos in ~ [16:21:43]
$ systemctl restart httpd.service

此时通过浏览器打开本地IP可以看到:

在CentOS7上搭建WordPress个人博客_第2张图片

安装数据库

之前一直在网上查阅如何搭建WordPress,大部分都是CentOS6的,CentOS7的数据库更名为mariadb,所以如果在CentOS7上执意要安装mysql的话,需要借助wget先下拉到本地,否则直接yum会出错。

接着安装mariadb:

# root @ VM_0_4_centos in ~ [16:24:05]
$ yum install mariadb-server -y

同样也启动同时设置为开机启动:

# root @ VM_0_4_centos in ~ [16:29:10]
$ systemctl start mariadb.service

# root @ VM_0_4_centos in ~ [16:29:38]
$ systemctl enable  mariadb.service

初始化数据库:

# root @ VM_0_4_centos in ~ [16:29:39]
$ 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密码,如果未设置直接回车即可)
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创建数据库和用户:

# root @ VM_0_4_centos in ~ [16:37:09] C:1
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, 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;//创建名为wpdb的数据库
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on wpdb.* to 'root'@'localhost' identified by 'password';//设置用名为root,密码为password
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方式下载到本地,如果没有安装wget需要先安装wget:

# root @ VM_0_4_centos in ~ [16:38:04]
$ yum install wget -y

# root @ VM_0_4_centos in ~ [16:38:04]
$ wget https://cn.wordpress.org/wordpress-4.9.1-zh_CN.tar.gz

解压缩WordPress安装吧并将所有文件拷贝到/var/www/html

# root @ VM_0_4_centos in ~ [16:51:18]
$ tar -xvzf wordpress-4.9.1-zh_CN.tar.gz

# root @ VM_0_4_centos in ~ [16:50:38]
$ cp -R wordpress/* /var/www/html/

//修改一下权限
# root @ VM_0_4_centos in ~ [16:51:09] C:1
$ chown -R root /var/www/html/

复制配置文件:

# root @ VM_0_4_centos in ~ [16:54:18] C:1
$ cd /var/www/html

# root @ VM_0_4_centos in /var/www/html [16:54:38]
$ cp wp-config-sample.php  wp-config.php

修改wp-config.php配置文件:

![php-install](img/php-install.png)# root @ VM_0_4_centos in /var/www/html [16:54:41]
$ vim wp-config.php

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

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

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

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

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

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

填写上述内容的时候要注意和之前配置数据库时的信息一致

重启数据库和httpd:

# root @ VM_0_4_centos in /var/www/html [16:58:19]
$ systemctl restart mariadb.service

# root @ VM_0_4_centos in /var/www/html [16:59:24]
$ systemctl restart httpd.service

此时访问WordPress安装界面:

在CentOS7上搭建WordPress个人博客_第3张图片
根据提示填写即可。

在CentOS7上搭建WordPress个人博客_第4张图片

登录之后进入设置界面,将访问端口设置为81(可以根据具体情况更改):

在CentOS7上搭建WordPress个人博客_第5张图片
然后再返回到服务器修改httpd的端口:

# root @ VM_0_4_centos in /var/www/html [17:03:30] C:1
$ vim /etc/httpd/conf/httpd.conf

#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See  for detailed information.
# In particular, see 
# 
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.  
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
# with ServerRoot set to '/www' will be interpreted by the
# server as '/www/log/access_log', where as '/log/access_log' will be
# interpreted as '/log/access_log'.

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path.  If you point
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# Mutex directive, if file-based mutexes are used.  If you wish to share the
# same ServerRoot for multiple httpd daemons, you will need to change at
# least PidFile.
#
ServerRoot "/etc/httpd"

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the 
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 81                                         //在这里修改

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf

#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
...

然后重启httpd服务:

# root @ VM_0_4_centos in /var/www/html [17:13:35]
$ systemctl restart httpd.service

大功告成啦。

总结

这篇博客基本上是根据个人的搜集汇总写出来的,个人的服务器已经成功,基本上按照流程来一般都没有问题,之后会持续更新问题解答。

你可能感兴趣的:(WordPress个人博客搭建,CentOS7,WordPress,CentOS7,后端搭建)