手把手教你在阿里云ECS上搭建惊艳的博客

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云服务器 ECS,u1 4核8GB 1个月
云服务器ECS,u1 2核4GB 1个月
简介: 手把手教你在阿里云ECS上搭建惊艳的博客

前言

相信大多数人都有自己搭建博客网站的想法,本文就手把手一步一步的进行,最终结果类似如下样式:

img

一、网站软件的选择

软件选择成熟而免费的WordPress,WordPress是一款能让您建立出色网站、博客或应用程序的开源软件。
可充分利用超过55,000个插件扩展WordPress,以让您的网站满足需求。您可以增加网店、相册、邮件列表、论坛、统计分析等。
当然也有非常多的网站模板可用。
msedge_N8WOrMSyDD

二、网站空间的选择

建网站,首先得有一个网上的空间,用于存放自己的网站。现在各种云都有相应的服务,一年也没多少钱,现在一般都有活动,如果是新用户建议买三年的。

1. 建议选择有名、大厂的

我用的阿里云的ECS,也推荐你试试,阿里云也有“试用装”:ECS免费试用_ECS试用中心_云服务器ECS_免费试用-阿里云 (aliyun.com)

msedge_b9YjoVl7Sd

个人的两核4G基本够用了,正好试用的列表里也有,试用一下:

msedge_Sb2QPfegev

目前各种云挺多,选择哪家呢,我目前用的阿里云的,为什么呢?

2. 上手容易,丰富而详实的文档

msedge_UGqbTxyXwN

3. 要稳定,少出问题

所以一定要选择几个大厂之一的,名声在外不易出问题。

4. 及时处理并有能力处理问题

这是非常重要的,出了问题的处理有足够的技术能力处理,并且处理及时。不止是ECS的使用,还包括其他包括域名备案等一系列问题。

例如我提交的一些工单都能很快的响应并给出处理方案。

msedge_MhWNybvriy

三、 环境准备

  • WordPress,官网地址:https://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 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,无需单独安装。

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 http://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

访问 http://服务器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 https://cn.wordpress.org/latest-zh_CN.tar.gz

下载完成之后,解压

tar -zxvf latest-zh_CN.tar.gz

拷贝到当前目录

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

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

总结

以上就是今天要讲的内容,手把手讲述了一个个人博客网站的搭建,你学会了么。

相关实践学习
ECS云服务器新手上路
本实验会自动创建一台ECS实例。首先,远程登陆ECS实例,并部署应用。然后,登陆管理控制台,并对这台ECS实例进行管理操作。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
5天前
|
存储 关系型数据库 数据库
给阿里云的建议和意见 一个云服务器架构是否可行
摘要(Markdown格式): 在修复阿里云服务器IPv4设置错误时遇到困难,导致服务器远程登录失败及外网访问受阻,耗时三天解决。建议阿里云更新文档,确保设置指导与实际情况一致,例如只需在路由表添加条目关联IPv4。此外,建议优化帮助页面,如采用折叠式设计减少干扰。服务器主要任务是数据分析、存储和分发,文中提出简化服务器框架,消除硬件软件复杂配置,利于初学者和独立开发者快速上手,降低时间成本。该设计旨在减少无用组件,节省资源,同时降低云服务商的人力和支持成本。期望云服务商考虑此类架构创新。目前未知是否有类似产品,期待业界反馈。
214 0
给阿里云的建议和意见 一个云服务器架构是否可行
|
3天前
|
弹性计算 运维 安全
阿里云ecs使用体验
整了台服务器部署项目上线
|
4天前
|
弹性计算
阿里云ECS的使用心得
本文主要讲述了我是如何了解到ECS,使用ECS的一些经验,以及自己的感悟心得
|
4天前
|
弹性计算
阿里云ECS使用体验
在申请高校学生免费体验阿里云ECS云服务器后的一些使用体验和感受。
|
4天前
|
弹性计算
阿里云ECS的使用心得
本文主要讲述了我是如何了解到ECS,使用ECS的一些经验,以及自己的感悟心得
|
6天前
|
弹性计算 运维 安全
阿里云ecs使用体验
整了台服务器部署项目上线
|
5天前
|
弹性计算
阿里云ECS使用体验
在申请高校学生免费体验阿里云ECS云服务器后的一些使用体验和感受。
|
6天前
|
弹性计算 网络安全 数据安全/隐私保护
阿里云服务器新手入门:注册账号、实名认证申请免费云服务器全流程
注册阿里云账号,选择注册方式如密码、扫码或手机号,并完成验证码验证。接着进行实名认证,提供必要个人信息或企业证件。然后,在控制台选择云服务器ECS,点击“免费试用”申请,配置实例并设置登录密码。创建后,通过远程连接工具如SSH登录,开始使用服务器进行软件安装、应用部署等操作。阿里云提供详细文档支持。别忘了点击链接以直接试用。
26 0
|
16天前
|
弹性计算 安全 前端开发
阿里云服务器ECS通用型、计算型和内存型详细介绍和性能参数表
阿里云ECS实例有计算型(c)、通用型(g)和内存型(r)三种,主要区别在于CPU和内存比例。计算型CPU内存比1:2,如2核4G;通用型为1:4,如2核8G;内存型为1:8,如2核16G。随着技术迭代,有第五代至第八代产品,如c7、g5、r8a等。每代实例在CPU型号和主频上相同,但性能有所提升。实例性能参数包括网络带宽、收发包能力、连接数等。具体应用场景如计算型适合高网络包收发、通用型适合企业级应用,内存型适合内存数据库等。详细信息可参阅阿里云ECS页面。
|
7天前
|
存储 编解码 网络协议
阿里云服务器计算型和通用型四代云服务器实例区别及选择参考
目前阿里云在售的云服务器中,计算型和通用型实例规格都包含了第5代、第6代、第7代和最新第八代倚天云服务器产品,例如计算型实例中有c5、c6、c7、c8y实例,而通用型实例有g5、g6、g7、g8y等实例,有的新手用户并不清楚这四代产品之间的差别,本文为大家展示这四代云服务器实例在规格、CPU(核)、内存(G)、计算、存储、内存以及不同配置的指标数据等方面为大家做个对比,让大家了解一下他们之间的不同,以供参考和选择。
阿里云服务器计算型和通用型四代云服务器实例区别及选择参考