【騰訊雲的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)(二)》

相關文章
相關標籤/搜索