1. 程式人生 > >使用阿里雲 ECS 快速部署 WordPress 部落格系統

使用阿里雲 ECS 快速部署 WordPress 部落格系統

今天在 阿里雲 ECS上 部署了一套 Lamp 系統,建了一個WordPress的網站,把操作過程記錄下來,文中所列指令碼可以直接應用。

廢話不多說直接開動,ECS雲服務購買可以點選 阿里雲ECS 雲主機 彈性計算 新人折扣

 

安裝Nginx

登入彈性雲伺服器。

執行以下命令,下載對應當前系統版本的Nginx包。

wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

 

執行以下命令,建立Nginx的yum倉庫。

rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

 

執行以下命令,安裝Nginx。

yum -y install nginx

 

執行以下命令,啟動Nginx並設定開機啟動。

systemctl start nginx
systemctl enable nginx

 

使用瀏覽器訪問 “http://伺服器IP地址”,顯示如下頁面,說明Nginx安裝成功

安裝MySQL

依次執行以下命令,安裝MySQL。

rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

yum -y install mysql-community-server

 

依次執行以下命令,啟動MySQL服務並設定開機自啟動。
systemctl start mysqld
systemctl enable mysqld

 

執行以下命令,獲取安裝MySQL時自動設定的root使用者密碼。

grep 'temporary password' /var/log/mysqld.log

 

回顯如下類似資訊。

2018-08-29T07:27:37.541944Z 1 [Note] A temporary password is generated for root@localhost: 2YY?3uHUA?Ys

 

執行以下命令,並按照回顯提示資訊進行操作,加固MySQL。
mysql_secure_installation

 

接下來按照提示操作:

Securing the MySQL server deployment.

Enter password for user root:    #輸入上一步驟中獲取的安裝MySQL時自動設定的root使用者密碼
The existing password for the user account root has expired. Please set a new password.

New password:  #設定新的root使用者密碼

Re-enter new password:   #再次輸入密碼
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N   #是否更改root使用者密碼,輸入N

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : Y   #禁止root遠端登入,輸入Y
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : Y   #是否刪除test庫和對它的訪問許可權,輸入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? (Press y|Y for Yes, any other key for No) : Y   #是否重新載入授權表,輸入Y
Success.

All done!

 

安裝PHP

依次執行以下命令,安裝PHP 7和一些所需的PHP擴充套件。

rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php70w-tidy php70w-common php70w-devel php70w-pdo php70w-mysql php70w-gd php70w-ldap php70w-mbstring php70w-mcrypt php70w-fpm

 

執行以下命令,驗證PHP的安裝版本。
php -v

 

執行以下命令,啟動PHP服務並設定開機自啟動。

systemctl start php-fpm
systemctl enable php-fpm

 

修改Nginx配置檔案以支援PHP

執行以下命令開啟配置檔案“default.conf”。

vim /etc/nginx/conf.d/default.conf

按i鍵進入編輯模式。

修改開啟的“default.conf”檔案。
在所支援的主頁面格式中新增php格式的主頁,如下所示:

 location / {
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;
    }

 

取消如下內容的註釋,並設定字型加粗部分為Nginx的預設路徑,如下圖所示:

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

 

按Esc鍵退出編輯模式,並輸入:wq儲存後退出。
執行以下命令,重新載入nginx的配置檔案。

service nginx reload

 

瀏覽器訪問測試

在/usr/share/nginx/html目錄下建立“info.php”的測試頁面。
執行以下命令建立並開啟“info.php”的測試檔案。

vim /usr/share/nginx/html/info.php

 

按i鍵進入編輯模式。

修改開啟的“info.php”檔案,將如下內容寫入檔案。

<?php
 phpinfo();
?>

 

按Esc鍵退出編輯模式,並輸入:wq儲存後退出。
使用瀏覽器訪問“http://伺服器IP地址/info.php”,顯示如下頁面,說明環境搭建成功。

建立資料庫

執行以下命令,並按照提示資訊輸入MySQL的root使用者,登入到MySQL命令列。

mysql -u root -p

 

執行以下命令,建立一個新的資料庫。

CREATE DATABASE wordpress;

 

其中,“wordpress”為資料庫名,可以自行設定。

執行以下命令,為資料庫建立使用者併為使用者分配資料庫的完全訪問許可權。

GRANT ALL ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'PASSWORD123';

 

其中,“wordpressuser”為資料庫使用者名稱,“PASSWORD123”為對應的帳戶密碼,可以自行設定。

執行以下命令,退出MySQL命令列。

exit

 

安裝WordPress

從WordPress官網獲取WordPress軟體包並上傳至/usr/share/nginx/html目錄。
命令列下載參考

wget http://wordpress.org/latest.tar.gz

由於WordPress官方網站被攻擊,導致國內使用者訪問WordPress網站的時候會提示429 Too Many Requests錯誤,
所以使用國內伺服器的朋友很難從官網下載新版版本的WordPress安裝包,這裡給大家分享一下從官網下載下來的新版版的WordPress安裝檔案,有需要的可以自己下載了。

可以使用SCP的方式上傳,

scp /Users/bingyue/Downloads/wordpress-5.3.2.tar.gz [email protected]:/opt  

 

後續操作軟體包以“wordpress-5.3.2-zh_CN.tar.gz”為例。

執行以下命令,解壓縮軟體包。

tar -xvf wordpress-5.3.2-zh_CN.tar.gz

 

解壓後生成一個“wordpress”的資料夾。

執行以下命令,設定解壓後的檔案許可權。

chmod -R 777 wordpress

 

瀏覽器訪問“http://伺服器IP地址/wordpress”進入安裝嚮導。

 

接下來使用配置好的MySQL進行建立即可。

 

&n