雙十一來了,有沒有打算自己某云買服務器搭建網站的,現在服務器挺便宜,建站也可以自己“免費”DIY,
本文手把手從零開始搭建,軟體選擇比較成熟的WordPress,作業系統選擇CentOS.
1. 環境準備
官網地址:https://cn.wordpress.org
- PHP 7.4或更高版本
- MySQL 5.6或更高版本,或MariaDB 10.1或更高版本
- Nginx或帶mod_rewrite模塊的Apache
- HTTPS支持
2. 安裝PHP8
2.1 首先更新一下
dnf upgrade
2.2 查看當前dnf庫中的php版本
dnf list mysql
我這里看到的是7.2版,版本過低,
php.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream
需要安裝個新的,
2.3 首先安裝Remi存盤庫
dnf install -y https://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,無需單獨安裝,
2.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”即可,
2.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,等待安裝完成,
2.6 安裝php-mysql擴展
dnf install php-mysql
2.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
3. 安裝mysql8.0
3.1 查看dnf庫中mysql版本
dnf list mysql
mysql.x86_64 8.0.26-1.module_el8.4.0+915+de215114 appstream
版本8.0,直接安裝,
3.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.3配置
啟動mysql
systemctl start mysqld
查看運行狀態
systemctl enable 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!
3.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退出,
4 安裝Nginx
4.1 查看dnf庫中Nginx版本
dnf list nginx
nginx.x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 appstream
版本有點低,安裝新版,
4.2 安裝Nginx1.20.1
輸入安裝命令
dnf install http://nginx.org/packages/centos/8/x86_64/RPMS/nginx-1.20.1-1.el8.ngx.x86_64.rpm
==========================================================================================
軟體包 架構 版本 倉庫 大小
==========================================================================================
安裝:
nginx x86_64 1:1.20.1-1.el8.ngx @commandline 819 k
事務概要
==========================================================================================
安裝 1 軟體包
選擇y等待安裝完成,
4.3 查看防火墻
查看是否已開放80埠,修改防火墻設定,某云需要修改安全組設定,
firewall-cmd --query-port=80/tcp
若回傳no則未開放,
開放80埠命令:
firewall-cmd --zone=public --add-port=80/tcp --permanent
使設定生效:
firewall-cmd --reload
4.4 訪問默認網站驗證安裝
啟動nginx
systemctl start nginx
查看運行狀態
systemctl status nginx
會看到包含“ Active: active (running)”字樣的成功提示,
設定開機自動啟動
systemctl enable nginx
訪問 http://服務器ip, 正常會是如下頁面

4.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
5. 安裝WordPress
5.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回車即可,
5.2 下載安裝包
安裝下載和解壓工具,如果已安裝則忽略
yum install wget
yum install tar
下載wordpress
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
下載完成之后,解壓
tar -zxvf latest-zh_CN.tar.gz
拷貝到當前目錄
cp -R wordpress/* /usr/share/nginx/html/
5.3 開始安裝
訪問"http://服務器ip/wp-admin/install.php",可以看到如下頁面:

點擊按鈕開始配置:

輸入準備好的資料庫相關資訊,
如果提示無法寫入wp-config.php,如下圖

手動創建wp-config.php檔案
cd /usr/share/nginx/html/
vi wp-config.php
按i鍵,粘貼網頁中給出的檔案內容,
按ESC,輸入:wq回車保存,
繼續安裝,在新頁面設定網站的相關資訊

點擊按鈕開始安裝,
安裝成功后,訪問http://服務器ip即可,
管理后臺地址:http://服務器ip/wp-admin
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/354699.html
標籤:其他
上一篇:cgb2109-day08
