手把手教你搭建驚艷的博客



前言

相信大多數人都有自己搭建博客網站的想法,本文就手把手一步一步的進行,最終結果類似如下樣式:

img

一、網站軟件的選擇

軟件選擇成熟而免費的WordPress,WordPress是一款能讓您建立出色網站、博客或應用程序的開源軟件。
可充分利用超過55,000個插件擴展WordPress,以讓您的網站滿足需求。您可以增加網店、相冊、郵件列表、論壇、統計分析等。
當然也有非常多的網站模板可用。
msedge_N8WOrMSyDD

二、網站空間的選擇

建網站,首先得有一個網上的空間,用於存放自己的網站。現在各種雲都有相應的服務,一年也沒多少錢,現在一般都有活動,如果是新用戶建議買三年的。

三、 環境準備

  • WordPress,官網地址://cn.wordpress.org
  • PHP 7.4或更高版本
  • MySQL 5.6或更高版本,或MariaDB 10.1或更高版本
  • Nginx或帶mod_rewrite模塊的Apache
  • HTTPS支持

四、 安裝PHP8

1. 首先更新一下

dnf upgrade

2. 查看當前dnf庫中的php版本

dnf list php

我這裡看到的是7.2版,版本過低,

php.x86_64     7.2.24-1.module_el8.2.0+313+b04d0a66     appstream

需要安裝個新的。

3. 首先安裝Remi存儲庫

dnf install -y //rpms.remirepo.net/enterprise/remi-release-8.rpm
remi-release-8.rpm           0 kB/s |  26 kB     00:01
依賴關係解決。
===================================================================================== 
軟件包              架構               版本            倉庫                 大小
=====================================================================================
安裝:
 remi-release      noarch             8.4-1.el8.remi  @commandline         26 k
安裝依賴關係:
 epel-release      noarch             8-11.el8        extras               24 k

會自動安裝依賴epel-release,無需單獨安裝。

4. 列出PHP module

dnf module list php

結果類似如下:

CentOS Linux 8 - AppStream
Name        Stream        Profiles                       Summary
php         7.2 [d]       common [d], devel, minimal     PHP scripting language
php         7.3           common [d], devel, minimal     PHP scripting language
php         7.4           common [d], devel, minimal     PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name        Stream         Profiles                      Summary
php         remi-7.2       common [d], devel, minimal    PHP scripting language
php         remi-7.3       common [d], devel, minimal    PHP scripting language
php         remi-7.4       common [d], devel, minimal    PHP scripting language
php         remi-8.0       common [d], devel, minimal    PHP scripting language
php         remi-8.1       common [d], devel, minimal    PHP scripting language

可以看到已經有了8.0和8.1版本,官網現在8.1還是RC版,準備安裝8.0。
期間會詢問導入各種公鑰,選擇「y」即可。

5. 安裝php 8.0

dnf module enable php:remi-8.0
dnf install php
===================================================================================
 軟件包            架構     版本                 倉庫                  大小
===================================================================================
安裝:
 php              x86_64   8.0.12-1.el8.remi     remi-modular         1.6 M
安裝依賴關係:
 apr              x86_64   1.6.3-11.el8          AppStream            125 k
 apr-util         x86_64   1.6.1-6.el8           AppStream            105 k
 centos-logos-httpd noarch 85.8-1.el8            base                  75 k
 httpd            x86_64   2.4.37-39.module_el8  AppStream            1.4 M
 httpd-filesystem noarch   2.4.37-39.module_el8  AppStream             39 k
 httpd-tools      x86_64   2.4.37-39.module_el8  AppStream            106 k
 libsodium        x86_64   1.0.18-2.el8          epel                 162 k
 libxslt          x86_64   1.1.32-6.el8          base                 250 k
 mailcap          noarch   2.1.48-3.el8          base                  39 k
 mod_http2        x86_64   1.15.7-3.module_el8   AppStream            154 k
 oniguruma5php    x86_64   6.9.7.1-1.el8.remi    remi-safe            210 k
 php-common       x86_64   8.0.12-1.el8.remi     remi-modular         1.2 M
安裝弱的依賴:
 apr-util-bdb     x86_64   1.6.1-6.el8           AppStream             25 k
 apr-util-openssl x86_64   1.6.1-6.el8           AppStream             27 k
 nginx-filesystem noarch   1:1.14.1-9.module_el8 AppStream             24 k
 php-cli          x86_64   8.0.12-1.el8.remi     remi-modular         4.7 M
 php-fpm          x86_64   8.0.12-1.el8.remi     remi-modular         1.6 M
 php-mbstring     x86_64   8.0.12-1.el8.remi     remi-modular         525 k
 php-opcache      x86_64   8.0.12-1.el8.remi     remi-modular         768 k
 php-pdo          x86_64   8.0.12-1.el8.remi     remi-modular         156 k
 php-sodium       x86_64   8.0.12-1.el8.remi     remi-modular          94 k
 php-xml          x86_64   8.0.12-1.el8.remi     remi-modular         238 k
啟用模塊流:
 httpd                     2.4
 nginx                     1.14

事務概要
================================================================================
安裝  23 軟件包

選擇y,等待安裝完成。

6. 安裝php-mysql擴展

逐一執行下面命令安裝擴展

dnf install php-mysql
dnf install php-gd
dnf install php-imagick
dnf install php-zip

7. 驗證安裝

輸入命令查看php版本:

 php -v
PHP 8.0.12 (cli) (built: Oct 19 2021 10:34:32) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.12, Copyright (c), by Zend Technologies

五、 安裝mysql8.0

1. 查看dnf庫中mysql版本

dnf list mysql
mysql.x86_64        8.0.26-1.module_el8.4.0+915+de215114     appstream

版本8.0,直接安裝。

2. 安裝Mysql8.0

輸入安裝命令,注意有「@」:

dnf install @mysql
依賴關係解決。
========================================================================================
 軟件包         架構     版本              倉庫                大小
========================================================================================
安裝組/模塊包:
 mysql-server  x86_64   8.0.26-1.module_el8.4.0+915+de215114   AppStream      25 M
安裝依賴關係:
 ...省略....
安裝模塊配置檔案:
 mysql/server
啟用模塊流:
 perl                   5.26
 perl-IO-Socket-SSL     2.066
 perl-libwww-perl       6.34

事務概要
========================================================================================
安裝  54 軟件包

選擇y等待安裝完成。

3.配置

啟動mysql

systemctl start mysqld

查看運行狀態

systemctl status mysqld

設為開機啟動

systemctl enable mysqld

運行配置嚮導

mysql_secure_installation

根據提示進行操作:

[root@localhost mysql]# mysql_secure_installation
## 默認已用空密碼登錄
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.

##是否安裝密碼強度驗證模塊,看自己需求
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: no

## 設置root的密碼
Please set the password for root here.
New password:
Re-enter new password:

## 是否刪除匿名用戶,可刪
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
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
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
 - 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
Success.
## 配置完畢
All done!

4. 為WordPress創建數據庫

登錄數據庫

mysql -uroot -p

提示輸入密碼,輸入剛設置的root的密碼。
創建數據庫,名稱自己定,例如wordpress

create database wordpress;

查看現有數據庫

show databases;

可以看到新建好的數據庫

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

輸入exit退出。

六、 安裝Nginx

1. 查看dnf庫中Nginx版本

dnf list nginx
nginx.x86_64      1:1.14.1-9.module_el8.0.0+184+e34fea82     appstream

版本有點低,安裝新版。

2. 安裝Nginx1.20

輸入安裝命令

dnf install //nginx.org/packages/centos/8/x86_64/RPMS/nginx-1.20.2-1.el8.ngx.x86_64.rpm
========================================================================================== 
軟件包            架構       版本                        倉庫                  大小
==========================================================================================
安裝:
 nginx           x86_64     1:1.20.2-1.el8.ngx          @commandline          819 k

事務概要
==========================================================================================
安裝  1 軟件包

選擇y等待安裝完成。

3. 查看防火牆

查看是否已開放80端口,修改防火牆設置。某雲需要修改安全組設置。

firewall-cmd --query-port=80/tcp

若返回no則未開放。
開放80端口命令:

firewall-cmd --zone=public --add-port=80/tcp --permanent

使設置生效:

 firewall-cmd --reload

4. 訪問默認網站驗證安裝

啟動nginx

systemctl start nginx

查看運行狀態

systemctl status nginx

會看到包含「 Active: active (running)」字樣的成功提示。

設置開機自動啟動

systemctl enable nginx

訪問 //服務器ip, 正常會是如下頁面
在這裡插入圖片描述

5. 啟用php支持

修改nginx配置文件

cd /etc/nginx/conf.d

備份默認的配置文件

cp default.conf default.conf.bak

修改default.conf

vi default.conf

按”i”鍵,找到如下代碼塊,默認是注釋狀態,去掉#號,並修改為如下配置

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html/;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

fastcgi_pass對應php-fpm的監聽配置, 配置文件為: /etc/php-fpm.d/www.conf,可查看驗證。

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php-fpm/www.sock

設置默認文件為index.php

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

修改完畢後,按ESC,輸入:wq保存並退出。
重啟nginx:

systemctl restart nginx

七、安裝WordPress

1. 訪問網站目錄

nginx的默認網站目錄在/usr/share/nginx/html/,訪問並驗證一下

cd /usr/share/nginx/html/
ll

可以看到兩個html文件,即默認的「Welcome to nginx!」頁面。

-rw-r--r--. 1 root root 494 5月  25 09:41 50x.html
-rw-r--r--. 1 root root 612 5月  25 09:41 index.html

可以刪除默認的index.html文件

rm -r index.html

詢問是否刪除,輸入y回車即可。

2. 下載安裝包

安裝下載和解壓工具,如果已安裝則忽略

dnf install wget
dnf install tar

下載wordpress

wget //cn.wordpress.org/latest-zh_CN.tar.gz

下載完成之後,解壓

tar -zxvf latest-zh_CN.tar.gz

拷貝到當前目錄

cp -R wordpress/* /usr/share/nginx/html/

3. 開始安裝

訪問”//服務器ip/wp-admin/install.php“,可以看到如下頁面:
在這裡插入圖片描述
點擊按鈕開始配置:
在這裡插入圖片描述
輸入準備好的數據庫相關信息。
如果提示無法寫入wp-config.php,如下圖
在這裡插入圖片描述
手動創建wp-config.php文件

cd /usr/share/nginx/html/
vi wp-config.php

按i鍵,粘貼網頁中給出的文件內容。
按ESC,輸入:wq回車保存。

繼續安裝,在新頁面設置網站的相關信息
在這裡插入圖片描述
點擊按鈕開始安裝。
安裝成功後,訪問//服務器ip即可。
管理後台地址://服務器ip/wp-admin

總結

以上就是今天要講的內容,手把手講述了一個個人博客網站的搭建,你學會了么。

Tags: