如何一步一步建立一个属于你自己的个人网站?
By Long Luo
本站目前采用 Hexo 作为后台系统,托管在Github上。此前我曾在很多地方安过家,最开始新浪和QQ空间上写过博客,后来到网易博客,再后来看到程序员都有自己的个人网站,于是2014年也新建了一个人网站,当时是买了域名和一个VPS,使用的LNMP架构。
在使用了流行的WordPress两年后看到码农的乐土,Jekyll ,一个以纯静态文件的博客系统。但后来我发现Jekyll
实在太慢,而且美观度也不够,于是投奔了在Hexo
。
LNMP (Linux + Nginx + MySQL + PHP)
Ubuntu 20.04
使用Nginx官方源安装Nginx。
Nginx安装前必要环境。
sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
导入官方nginx签名密钥。
curl https://nginx.org/keys/nginx_signing.key | gpg –dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
设置apt仓库。
echo “deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu lsb_release -cs
nginx” | sudo tee /etc/apt/sources.list.d/nginx.list
更新软件包列表并安装nginx。
说明 默认安装最新稳定版本Nginx,如果对版本有要求可以使用sudo apt list -a nginx搜索支持的Nginx版本并将安装命令替换为具体版本(例:安装1.22.1-1~focal版本,修改命令为sudo apt install -y nginx=1.22.1-1~focal)。
sudo apt update -y && sudo apt install -y nginx
安装MySQL数据库并设置密码。
更新软件包列表并安装MySQL服务器。
sudo apt update -y && sudo apt install -y mysql-server
将MySQL的配置文件中监听地址从127.0.0.1(即只监听本地连接)更改为0.0.0.0(即监听所有可用网络接口),从而允许远程连接到MySQL服务器。
sudo sed -i “s/127.0.0.1/0.0.0.0/” /etc/mysql/mysql.conf.d/mysqld.cnf
修改数据库root用户主机部分从localhost更改为%以允许从任何地址连接,同时修改root用户的密码和身份认证插件。您需要将命令中
重要 由于本地root用户的默认身份认证插件是auth_socket,如果命令执行后提示输入密码,请直接按回车跳过。
sudo mysql -uroot -p -e “ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH caching_sha2_password BY ‘
重启MySQL数据库服务使配置生效。
sudo systemctl restart mysql
安装PHP。
更新软件包,安装software-properties-common包,并添加PPA仓库ppa:ondrej/php。
sudo apt update && sudo apt install -y software-properties-common && sudo add-apt-repository -y ppa:ondrej/php
安装PHP8.4及相关组件,包括FPM和MySQL扩展。
说明 您可以通过sudo apt search php查看所有支持安装的PHP版本,安装其他版本需要修改命令中对应版本号(例:安装PHP8.1需修改命令sudo apt install -y php8.1 php8.1-fpm php8.1-mysql)。
sudo apt install -y php8.4 php8.4-fpm php8.4-mysql
验证LNMP环境。
查询php-fpm配置文件默认监听地址,
sudo grep ‘^listen =’ /etc/php/
sudo grep ‘^listen =’ /etc/php/8.4/fpm/pool.d/www.conf
如果返回sock文件地址说明默认监听sock文件。
如果返回127.0.0.1:9000说明默认监听本地9000端口。
listen = /run/php/php8.4-fpm.sock
编辑 /etc/nginx/conf.d/default.conf 文件,在server内填写PHP转发规则,
重要 如果监听的sock文件,需要sock文件的权限设置为允许读写,您可以使用以下命令来更改权限sudo chmod 666
location / { index index.php index.html index.htm; } location ~ .php$ { root /usr/share/nginx/html; fastcgi_pass
location / { index index.php index.html index.htm; } location ~ .php$ { root /usr/share/nginx/html; fastcgi_pass unix:/run/php/php8.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME \(document_root\)fastcgi_script_name; include fastcgi_params; }
重启Nginx服务使修改后的配置文件生效。
sudo systemctl restart nginx
远程访问MySQL数据库
运行以下命令后,输入root用户的密码登录MySQL。
sudo mysql -uroot -p
依次运行以下命令,创建远程登录MySQL的账号,并允许远程主机使用该账号访问MySQL。
本示例账号为dmsTest、密码为Ecs@123****。
实际创建账号时,需将示例密码Ecs@123
更换为符合要求的密码,并妥善保存。密码要求:长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。可以使用以下特殊符号:
()` ~!@#$%^&*-+=|{}[]:;’<>,.?/
创建数据库用户dmsTest,并授予远程连接权限。
create user ‘frank’@‘%’ identified by ‘wp291563pd’;
为dmsTest用户授权数据库所有权限。
grant all privileges on . to ‘frank’@‘%’;
刷新权限。
flush privileges;
运行以下命令,为WordPress网站创建一个名称为wordpress的数据库。
create database wordpress;
create user ‘wpuser’@‘localhost’ identified by ‘25789pwdb’;
cd /usr/share/nginx/html
sudo wget https://cn.wordpress.org/latest-zh_CN.zip
启动nginx服务
sudo systemctl enable nginx sudo systemctl start nginx
enable:设置 Nginx 服务在系统启动时自动启动。 start:立即启动 Nginx 服务。
Nginx服务是否启动成功
sudo systemctl status nginx
status:查看 Nginx 服务的状态。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15● nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2025-01-03 19:25:38 CST; 1h 47min ago
Docs: https://nginx.org/en/docs/
Main PID: 2658 (nginx)
Tasks: 3 (limit: 1930)
Memory: 3.3M
CPU: 14ms
CGroup: /system.slice/nginx.service
├─2658 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
├─2659 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
└─2660 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Jan 03 19:25:38 iZwz9bi9yxqor74jqvhsr1Z systemd[1]: Starting nginx - high performance web server...
Jan 03 19:25:38 iZwz9bi9yxqor74jqvhsr1Z systemd[1]: Started nginx - high performance web server.
3 当修改了配置的时候,需要重启nginx,使配置生效的时候使用一下命令:
sudo systemctl reload nginx
- Nginx的配置文件存放的位置?
在Ubuntu环境中,Nginx的存放位置在以下目录中:
mysql -u wpuser -p -h localhost wordpress
Jekyll
$ git –version git version 1.7.1
$ ssh-keygen -t rsa -C “youremail@example.com”
id_rsa.pub
ssh -t git@github.com
$ git config –global user.name “Your Name” $ git config –global user.email “email@example.com”1
2
3
4
5
6
7The authenticity of host 'github.com (192.30.253.112)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi longluo! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
修改SSH密码。登录ssh后, 通过passwd命令修改即可,命令格式:1
passwd {用户名}
出现:(current) UNIX password: 然后输入当前系统登陆用户的密码 回车 出现:New password: 再输入新密码(新的密码必须是字母数字都有,不然的话不成功)