【腾讯云的1001种玩法】几种在腾讯云创建WordPress的方法(Linux)(一)

版权声明:本文由张宁 原创文章,转载请注明出处: 
文章原文连接:https://www.qcloud.com/community/article/928869001488201231php

来源:腾云阁 https://www.qcloud.com/communityhtml

 

引言:腾讯云做为国内一流的云服务提供商,云服务器CVM的使用可谓是“一键成站”,十分方便。今天咱们就来介绍一下几种在腾讯云创建WordPress的方法。mysql

这里,咱们仅介绍Linux下安装WordPress等内容托管开源程序的示例,对于在Windows下的环境配置与开源程序安装大同小异,只不过Windows下图形界面多一些使得让人看上去没那么难而已。nginx

使用此教程前先修知识:Linux简单命令Vim编辑器的使用(点击连接便可查看相关教程)sql

【常规安装——LAMP或LNMP安装】

前奏:环境检查

  1. 操做系统:Linux(本例使用的大部分在CentOS 7.x x64与Ubuntu 14.04.1 LTS 64位上可行)
  2. 服务架构:LAMP或LNMP数据库

    注:此处LAMP为Linux+Apache+MariaDB+PHP,LNMP为Linux+Nginx+MySQL+PHPapache

  3. 软体版本:Apache 2.四、MariaDB5.五、PHP5vim

LAMP在CentOS上的配置

这一节是按照已有域名来写的,若是暂时没有域名或者只是想配置单网站,能够先看下一节“LNMP在CentOS上的配置”centos

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,是最流行的Web服务器端软件之一。Apache 2.4版本是Apache一重大升级版本,新版改进了缓存、代理模块,会话控制,改进异步读写、增长proxy_fcgi各方面支持等,有大幅度提高并发性能。浏览器

下面,咱们就介绍一下在Apache配置多域名或已有域名模式的WordPress,以供参考。

更新系统

# yum update 

安装并配置 Apache 网络服务器

CentOS 7 中默认的是 Apache 2.4 版本,Apache 官网有份很好的 vhost.conf 配置实例

安装

# yum install -y httpd 

备份配置文件(建议对于全部的配置文件,作任何更改前都先备份一份,以便应对未知错误)

# mkdir ~/zningfbak # cp -R /etc/httpd ~/zningbak 

其中  表示当前登陆用户的用户文件夹;-R 参数表示递归到全部子目录

配置虚拟主机(/etc/httpd/conf.d/vhost.conf )

# vim /etc/httpd/conf.d/vhost.conf 

文件内须要填写的内容:

# # 主机 1 # zning.net # <VirtualHost *:80> ServerAdmin admin@zning.net ServerName zning.net ServerAlias www # 注意下面这行末尾不要带 / DocumentRoot /srv/www/zning.net/html <Directory "/srv/www/zning.net/html/"> Options FollowSymLinks # 下一行这样设置就能够在网站文件的目录中使用 .htaccess AllowOverride All # 下一行是替代 Allow from all 的新机制 Require all granted </Directory> ErrorLog /srv/www/zning.net/logs/error.log CustomLog /srv/www/zning.net/logs/access.log combined ServerSignature Off </VirtualHost> # # 主机 2 # qcloud.zning.net # 这是另外一个位于同一服务器的网站,若是不须要则删除此段配置 # <VirtualHost *:80> ServerAdmin admin@zning.net ServerName test.zning.net ServerAlias test DocumentRoot /srv/www/qcloud.zning.net/html <Directory "/srv/www/qcloud.zning.net/html/"> Options FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog /srv/www/qcloud.zning.net/logs/error.log CustomLog /srv/www/qcloud.zning.net/logs/access.log combined ServerSignature Off </VirtualHost> # # 主机 3 # 为了之后给访问 phpMyAdmin 的时候用,也能够是别的端口,如 4444 Listen 3366 # # phpMyAdmin,访问地址:http://12.34.56.78:3366/phpMyAdmin # <VirtualHost 12.34.56.78:3366> ServerAdmin admin@zning.net DocumentRoot /srv/www/phpmyadmin/html <Directory "/srv/www/phpmyadmin/html/"> Options FollowSymLinks AllowOverride None Require all granted </Directory> ErrorLog /srv/www/phpmyadmin/logs/error.log CustomLog /srv/www/phpmyadmin/logs/access.log combined ServerSignature Off </VirtualHost> 

虽然配置文件写好了,可是还不能启动 httpd 进程,由于上面设置的各个文件夹(网站目录)尚未建立。

建立各个虚拟主机的文件夹(根据须要增长或删除),

# 主机 1 的 # mkdir /srv/www/zning.net/html -p # mkdir /srv/www/zning.net/logs # 主机 2 的 # mkdir /srv/www/qcloud.zning.net/html -p # mkdir /srv/www/qcloud.zning.net/logs # 主机 3 的 # mkdir /srv/www/phpmyadmin/html -p # mkdir /srv/www/phpmyadmin/logs 

为了可以在系统启动时自动运行 Apache 服务器,须要运行下面的指令:

# systemctl enable httpd 

输出相似于,

# Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. 

而后启动 Apache 服务,

# systemctl start httpd 

若是上述指令提示说原本已经启动了 httpd,则从新加载它,

# systemctl reload httpd 

如今须要将 http 服务加入防火墙以容许外部访问。先启用防火墙服务,若是防火墙默认没有启动,则下述指令会提示错误,“FirewallD is not running”。

# systemctl enable firewalld && systemctl start firewalld 

其中 && 表示两条指令依次执行。

启动后再从新执行下面的指令:

将 HTTP 默认使用的端口 80 加入到防火墙容许列表里

# firewall-cmd --add-service=http --permanent 

其中,–permanent 参数表示这是一条永久防火墙规则,若是不加这个参数则重启系统后就没有这条规则了。

而对于自定义的用于 phpMyAdmin 的 3366 端口,也须要添加相应的防火墙规则。由于是非标准端口,直接用数字表示便可,

# firewall-cmd --zone=public --add-port=3366/tcp --permanent 

重启 Firewalld 使该规则生效,

# systemctl restart firewalld 

若是要查看加入后的防火墙规则,使用指令,

# firewall-cmd --list-all 

显示结果相似于,

public (default)
  interfaces: sources: services: dhcpv6-client http ssh ports: 3366/tcp masquerade: no forward-ports: icmp-blocks: rich rules: 

若是已经作好了 DNS 域名解析,如今用浏览器打开域名应该可以看到 Apache 的测试页面了。

Apache 的测试页面

安装和配置 MariaDB 数据库服务

MariaDB 是在 MySQL 基础上重建的一个数据库软件,各 Linux 发行版都陆陆续续从 MySQL 切换到了 MariaDB。CentOS 从 7 开始默认使用 MariaDB。

安装

# yum install -y mariadb-server mariadb 

加入随系统启动

# systemctl enable mariadb 

输出结果相似于,

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. 

启动 MariaDB 守护进程(mysqld)

# systemctl start mariadb 

其默认用户名仍是 mysql,

# top -u mysql 

能够查看内存占用状况。

安全配置 MariaDB

使用 MariaDB 内建的安全配置脚本进行配置

# mysql_secure_installation 

这里须要配置 mysql 根用户和密码、清除其余用户、清除不须要的数据库等。输出相似于下面的执行过程,其中须要咱们从键盘输入的内容用**注释出来了:

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): 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 **此处输入y** 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 **此处输入y** ... Success! 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] y **此处输入y** ... Success! 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 **此处输入y** - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y **此处输入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! 

而后使用 MySQL 的 root 账户(不一样于 Linux 的 root 账户,刚才设置密码了)登陆进去

# mysql -u root -p 

输入密码后回车,下面是输出示例,能够看到命令提示符变为 MariaDB [(none)]>

Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 20 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> 

建一个新数据库给 WordPress 用(这里取名为 wpzning,也能够用别的名字)

MariaDB [(none)]> create database wpzning; 

建立一个新用户,并将该数据库的权限赋给他(这里只是举例,用户名为 zningwp_us,密码为 zningwp2016)

MariaDB [(none)]> grant all on wpzning.* to 'zningwp_us' identified by 'zningwp2016'; 

更新权限

MariaDB [(none)]> flush privileges; 

退出数据库

MariaDB [(none)]> quit 

执行大致输出以下:

MariaDB [(none)]> create database wpzning; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all on wpzning.* to 'zningwp_us' identified by 'zningwp2016'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> quit Bye 

备份配置文件,

# cp /etc/my.cnf ~/zningbak/my.cnf.bak 

其它的先不作了,回头用图形界面的 phpMyAdmin 来作。

安装和配置 PHP

安装 PHP5

# yum install -y php 

备份配置文件 /etc/php.ini,还有 php.conf 以及 10-php.conf

# cp /etc/php.ini ~/zningbak/php.ini.bak # cp /etc/httpd/conf.d/php.conf ~/zningbak/httpd/conf.d/php.conf.bak # cp /etc/httpd/conf.modules.d/10-php.conf ~/zningbak/httpd/conf.modules.d/10-php.conf.bak 

并确保 /etc/php.ini 中有下面的语句(不一样的就修改,没有的就添加,某些数值能够后再调整,这是针对一个简单的运行 WordPress 的服务器的配置):

error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR display_errors = Off log_errors = On max_execution_time = 300 memory_limit = 32M 

安装 PHP-MYSQL

为了在 PHP 中使用 MySQL,还须要安装这个 php-mysql 包:

# yum install -y php-mysql 

安装和配置 phpMyAdmin (选做)

Remi 安装源上有最新的 PHP、MySQL 以及 phpMyAdmin 的 Yum 安装包,能够方便安装、更新。可是正在使用的 Linux 发行版 CentOS 7 上的软件包可能版本上要求不同,因此使用 Yum 安装源优先级插件来进行控制。

安装使用 EPEL REPOSITORY
选择合适的源地址来安装,参考方法:安装使用 EPEL 安装源。

到 EPEL 主页:

http://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F

找到 The newest version of ‘epel-release’ for EL7,点击打开新页面,复制 epel-release-latest-7.noarch.rpm 的连接(数字可能有变化,当前版本是 7)。采用下面的方法直接安装:

# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 

安装PHPMYADMIN

# yum install -y phpmyadmin 

配置

phpMyAdmin 的默认安装目录是 /usr/share/phpMyAdmin,同时会在 Apache 的配置文件目录中自动建立虚拟主机配置文件 /etc/httpd/conf.d/phpMyAdmin.conf (区分大小写)。

一样的,先备份配置文件以防修改出现错误,

# cp /etc/httpd/conf.d/phpMyAdmin.conf ~/zningbak/httpd/conf.d/phpMyAdmin.conf.bak 

而后修改配置文件(vim /etc/httpd/conf.d/phpMyAdmin.conf)中有设置:

Alias /phpMyAdmin /usr/share/phpMyAdmin Alias /phpmyadmin /usr/share/phpMyAdmin 

也就是说,咱们能够经过绑定到 Apache 服务器上的任何域名访问 phpMyDdmin。好比这里能够经过 zning.net/phpmyadmin 或者 zning.net/phpMyAdmin 访问。可是这样一来,phpMyAdmin 的内容就与网站内容混到一块儿了,感受 phpMyAdmin 成了网站的一个目录。但实际上咱们并不但愿别人也去访问这个页面。因此咱们使用习惯的地址加端口 3366(而不是默认的 80 端口,换成本身喜欢的端口就行,再好比 3344)的方式,即 IP:port/phpMyAdmin 的连接形式访问 phpMyAdmin,加 # 注释掉上面的 Apache Alias 规则:

#Alias /phpMyAdmin /usr/share/phpMyAdmin #Alias /phpmyadmin /usr/share/phpMyAdmin 

并将

<Directory /usr/share/phpMyAdmin/> ......... </Directory> 

里面的

Require ip 127.0.0.1 Require ip ::1 

改为

Require all granted 

保存退出,使之能够从任何地方均可以访问。若是本地电脑是固定 IP 的,为了安全,也能够将上面的Require all granted 改为 Require ip <your-local-ip>。咱的 ADSL 就享受不到这样的待遇了。

由于在前面建立虚拟主机配置文件 /etc/httpd/conf.d/vhost.conf 的时候已经为 phpMyAdmin 配置了一个端口为 2082 的虚拟主机,只能经过该虚拟主机(端口)访问 phpMyAdmin。由于/srv/www/phpmyadmin/public_html 配置为 phpMyAdmin 的虚拟主机目录,为该目录建立符号链接到 phpMyAdmin 安装目录(/usr/share/phpMyAdmin):

# ln -sf /usr/share/phpMyAdmin /srv/www/phpmyadmin/html 

备份而后修改 phpMyAdmin 的配置文件,

# cp /etc/phpMyAdmin/config.inc.php ~/zningbak/config.inc.php.bak 

编辑文件:

# vim /etc/phpMyAdmin/config.inc.php 

找到其中的以下代码段:

$cfg['blowfish_secret'] = '这里随便填上一串乱七八糟的字符便可,字母、数字等,长度最好大于 16 个'; 

按照上面的提示填写 blowfish_secret 字符串。其余的不用修改,保存并退出。

重起 APACHE 使配置生效

# systemctl restart httpd 

如今就能够经过 ip:port/phpMyAdmin (将 IP 换为你腾讯云服务器的 IP,端口为前面设置的 phpmyadmin 的虚拟主机端口,注意大小写)访问 phpMyAdmin 了。

使用前面建立的用户名和密码(2.3.4 中 grant 语句中包含的用户名和密码)登陆 phpMyAdmin。

###安装WordPress

域名配置

首先配置域名DNS,这里以DNSPod为示例演示,截图中也有对相关记录的解释,接下来的其余方法也可经过此教程来配置域名解析。

DNSPod增长解析如图:

DNSPod增长解析

DNSPod解析详解如图:

DNSPod解析详解

安装

转移目录到相关目录

# cd /srv/www/qcloud.zning.net/html 

下载WordPress

# wget https://cn.wordpress.org/wordpress-4.5.2-zh_CN.tar.gz 

解压文件

# tar zxvf wordpress-4.5.2-zh_CN.tar.gz 

移动文件

# mv /srv/www/qcloud.zning.net/html/wordpress/* /srv/www/qcloud.zning.net/html 

访问网址qcloud.zning.net,进行最后一步的安装。

出现以下图所示页面,单击继续:

WordPress最后一步的安装

输入已经建立的数据库信息:

WordPress最后一步的安装

这一步出错的缘由是,网站自己对html文件夹没有读写权限,具体解决方案能够查看本节参考资料连接中关于网站用户权限的讲解。此处咱们能够按照提示,经过ssh,输入vim /srv/www/qcloud.zning.net/html/wp-config.php来建立,并复制WordPress已经提供的内容,保存继续便可

WordPress最后一步的安装

到这里说明已经链接了数据库,提供网站管理信息就能够继续安装咯。

WordPress最后一步的安装

哇咔咔,这个页面不用解释了吧~

WordPress最后一步的安装

仪表盘控制后台界面:

WordPress最后一步的安装

首页截图:

WordPress最后一步的安装

LNMP在CentOS上的配置

Nginx是一款轻量级的Web 服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler使用。其特色是占有内存少,并发能力强。

下面,咱们就介绍一下在Nginx配置单网站模式的WordPress,以供参考。

安装Nginx服务程序

安装Nginx:

# yum install -y nginx 

使Nginx可以开机自启:

# systemctl enable nginx 

输出结果相似于,

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. 

从新启动Nginx:

# systemctl restart nginx.service 

安装完成后,访问IP地址,出现以下图页面的欢迎界面,即说明已经完成安装了。

安装完成Nginx的页面

在这个页面咱们看到

This is the default index.html page that is distributed with nginx on Fedora. It is located in /usr/share/nginx/html.You should now put your content in a location of your choice and edit the root configuration directive in the nginx configuration file /etc/nginx/nginx.conf.

这两句话,这里告诉咱们Nginx默认的配置文件在哪里,以及访问路径在哪里,也就是咱们须要放置WordPress的路径在哪里。先记下来,等会有用

这里的Nginx是最简单的安装方法,其实最合适的安装方法仍是在本地环境交叉编译完成后进行的安装,也不算特别麻烦,教程详见:nginx服务器安装及配置文件详解

安装 PHP 服务程序和 MariaDB 数据库服务程序

此步与上节相同,请参照上节相关子节的步骤进行便可。

不过按照上节步骤安装完PHP以后,须要安装一个php-fpm插件才行

# yum install -y php-fpm 

建立守护进程,并设置自启并启动:

# systemctl enable php-fpm # systemctl start php-fpm 

输出以下的相似内容:

Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. 

配置完成并启动后,咱们经过以下命令能够看到进程的监听状态:

# netstat -antl|grep 9000 # ps -ef|grep php-cgi 

输出以下的相似内容:

[root@QCLOUD share]# ps -ef|grep php-cgi root  12838  8024  0 10:23 pts/0 00:00:00 grep --color=auto php-cgi [root@QCLOUD share]# netstat -antl|grep 9000 tcp  0  0 127.0.0.1:9000 0.0.0.0:* LISTEN 

安装WordPress

首先配置nginx.conf,经过以下命令进入编辑器:

# vim /etc/nginx/nginx.conf 

location / { index index.html index.php; } 

中添加

index index.html index.php; 

并在}后另起一行,添加以下内容:

location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 

添加完成后,server代码区段的代码状况以下:

location / { index index.html index.php; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 

而后,进入刚才所列的路径:

# cd /usr/share/nginx/html 

下载WordPress

# wget https://cn.wordpress.org/wordpress-4.5.2-zh_CN.tar.gz 

这里可能受服务器影响,北京机房下载速度不错,上海可能慢点。

解压文件

# tar zxvf wordpress-4.5.2-zh_CN.tar.gz 

更改与备份html文件夹现有文件:

# mv index.html index.html.bak 

移动文件

# mv /usr/share/nginx/html/wordpress/* /usr/share/nginx/html 

访问你服务器的IP地址,进行最后一步的安装。

安装状况同上节相关子节,请参见上一节的相关子节

小结

这是最传统的安装方法,学习这样的安装方法主要仍是为了熟悉Linux环境的命令操做与文本编辑。能够有效学习各类组件的调用执行原理。建议人人都会。

请继续阅读《【腾讯云的1001种玩法】几种在腾讯云创建WordPress的方法(Linux)(二)》

相关文章
相关标签/搜索