mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
600 字
2 分钟
新服务器开荒
2025-01-10

背景介绍#

我通过codebuddy新年活动获取了一个免费的CentOS 9服务器

服务器架构配置解析#

[root@VM-0-4-centos ~]# echo "=== CPU ===" && lscpu | grep -E "Model name|CPU\(s\)|Thread"
echo "=== 内存 ===" && free -h
echo "=== 磁盘 ===" && df -h
echo "=== 系统 ===" && uname -a
=== CPU ===
CPU(s): 2
On-line CPU(s) list: 0,1
Model name: Intel(R) Xeon(R) Platinum 8255C CPU @ 2.50GHz
BIOS Model name: 3.0
Thread(s) per core: 1
NUMA node0 CPU(s): 0,1
=== 内存 ===
total used free shared buff/cache available
Mem: 1.7Gi 266Mi 939Mi 6.0Mi 504Mi 1.3Gi
Swap: 0B 0B 0B
=== 磁盘 ===
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 856M 24K 856M 1% /dev/shm
tmpfs 343M 4.8M 338M 2% /run
/dev/vda1 50G 2.8G 45G 6% /
tmpfs 172M 0 172M 0% /run/user/0
=== 系统 ===
Linux VM-0-4-centos 5.14.0-444.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Apr 26 18:25:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

修改主机名#

  • 默认的那些服务器厂商都会给你一个主机名,在进行ssh的时候,显示老长一串,影响观感,所以要改一下主机名
# 需要先获取管理员权限,使用root账号登录
hostnamectl set-hostname 你想要的新名字
# 设置成功后退出
exit
# 或者执行
hostname
# 如果还不生效就重启ssh对话

AWS使用Root账号登录#

AWS 的默认安全策略,禁止直接用 root SSH 登录。解决方案有两种:

开启 root SSH 登录(文件传输问题的根本解法)#

用 ec2-user 登录后执行: ① 给 root 设置 authorized_keys(复用 ec2-user 的密钥)

sudo -i
cp /home/ec2-user/.ssh/authorized_keys /root/.ssh/
chown root:root /root/.ssh/authorized_keys

② 修改 SSH 配置允许 root 登录(有的服务器不需要做这一步)

sudo vi /etc/ssh/sshd_config
# 如果上面这个里面没有PermitRootLogin配置,那可能在下面这些文件中
ls /etc/ssh/sshd_config.d/
cat /etc/ssh/sshd_config.d/*.conf

找到这行改成:

PermitRootLogin yes

③ 重启 SSH 服务

sudo systemctl restart sshd

安装&配置 nginx#

1. 安装 Nginx#

# CentOS 9 直接用 dnf 安装
dnf install nginx -y

2. 启动 & 设置开机自启#

# 启动 Nginx
systemctl start nginx
# 设置开机自启
systemctl enable nginx
# 查看运行状态
systemctl status nginx

3. 防火墙放行端口#

# 放行 HTTP / HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
# 验证
firewall-cmd --list-all

4. 核心配置文件结构#

/etc/nginx/
├── nginx.conf # 主配置文件
├── conf.d/ # 自定义站点配置(推荐在这里操作)
└── default.d/

nginx 配置测试#

以之前做过的 zjp 代理为例子进行配置

systemctl start nginx # 启动
systemctl stop nginx # 停止
systemctl reload nginx # 热重载配置(推荐)
systemctl restart nginx # 重启
nginx -t # 检查配置语法
nginx -v # 查看版本

安装 PostgreSQL#

postgresql 客户端安装#

# 第一步:安装 PostgreSQL 官方源(必须)
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 第二步:禁用系统自带的 PostgreSQL 模块
sudo dnf -qy module disable postgresql
# 第三步:安装 psql 16 客户端(就是你要的 16.13)
sudo dnf install -y postgresql16
# 第四步:验证版本
psql -V # 输出 psql (PostgreSQL) 16.13 表示已经成功了
  • 执行示例
[root@ma ~]# sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Last metadata expiration check: 0:10:51 ago on Tue May 12 02:29:13 2026.
pgdg-redhat-repo-latest.noarch.rpm 559 kB/s | 13 kB 00:00
Dependencies resolved.
====================================================================================================================
Package Architecture Version Repository Size
====================================================================================================================
Installing:
pgdg-redhat-repo noarch 42.0-64.rhel9PGDG @commandline 13 k
Transaction Summary
====================================================================================================================
Install 1 Package
Total size: 13 k
Installed size: 16 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : pgdg-redhat-repo-42.0-64.rhel9PGDG.noarch 1/1
Verifying : pgdg-redhat-repo-42.0-64.rhel9PGDG.noarch 1/1
Installed:
pgdg-redhat-repo-42.0-64.rhel9PGDG.noarch
Complete!
[root@ma ~]# sudo dnf -qy module disable postgresql
Importing GPG key 0x08B40D20:
Userid : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
From : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
Userid : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
From : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
Userid : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
From : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
Userid : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
From : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
Userid : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
From : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Importing GPG key 0x08B40D20:
Userid : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
From : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
[root@ma ~]# sudo dnf install -y postgresql16
Last metadata expiration check: 0:00:11 ago on Tue May 12 02:40:18 2026.
Dependencies resolved.
====================================================================================================================
Package Architecture Version Repository Size
====================================================================================================================
Installing:
postgresql16 x86_64 16.13-1PGDG.rhel9.7 pgdg16 1.8 M
Installing dependencies:
postgresql16-libs x86_64 16.13-1PGDG.rhel9.7 pgdg16 335 k
Transaction Summary
====================================================================================================================
Install 2 Packages
Total download size: 2.1 M
Installed size: 11 M
Downloading Packages:
(1/2): postgresql16-16.13-1PGDG.rhel9.7.x86_64.rpm 19 MB/s | 1.8 MB 00:00
(2/2): postgresql16-libs-16.13-1PGDG.rhel9.7.x86_64.rpm 3.2 MB/s | 335 kB 00:00
--------------------------------------------------------------------------------------------------------------------
Total 20 MB/s | 2.1 MB 00:00
PostgreSQL 16 for RHEL / Rocky Linux / AlmaLinux 9 - x86_64 2.4 MB/s | 2.4 kB 00:00
Importing GPG key 0x08B40D20:
Userid : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
From : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : postgresql16-libs-16.13-1PGDG.rhel9.7.x86_64 1/2
Running scriptlet: postgresql16-libs-16.13-1PGDG.rhel9.7.x86_64 1/2
Installing : postgresql16-16.13-1PGDG.rhel9.7.x86_64 2/2
Running scriptlet: postgresql16-16.13-1PGDG.rhel9.7.x86_64 2/2
Verifying : postgresql16-16.13-1PGDG.rhel9.7.x86_64 1/2
Verifying : postgresql16-libs-16.13-1PGDG.rhel9.7.x86_64 2/2
Installed:
postgresql16-16.13-1PGDG.rhel9.7.x86_64 postgresql16-libs-16.13-1PGDG.rhel9.7.x86_64
Complete!
[root@ma ~]# psql -V
psql (PostgreSQL) 16.13

PostgreSQL 数据库服务安装#

# 安装 PostgreSQL 16 服务端(完整版)
sudo dnf install -y postgresql16-server
# 初始化数据库(必须执行一次)
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
# 启动并设置开机自启
sudo systemctl enable --now postgresql-16
# 检查是否启动成功
sudo systemctl status postgresql-16

执行完上面的命令之后,看到active (running)就证明服务安装且启动成功了

开启外部访问#

# 1. 开启外网访问
echo "listen_addresses = '*'" >> /var/lib/pgsql/16/data/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/16/data/pg_hba.conf
# 2. 重启生效
systemctl restart postgresql-16

pg 数据库迁移#

  • 我有个限时的服务器(记作A服务器),我在上面搭建了一个pg服务器存了一些数据,现在快过期了,我又办了个B服务器,我现在想把A服务器里的pg迁移到B服务器里面

1. 在B服务器里安装同版本 pg#

  • 我的A服务器里的pg是 16.3 版本的,最好的方式就是环境相同,这样不容易出现bug 安装过程:[[#安装 PostgreSQL]]

2. 导出A服务器里的 pg 数据#

  • 需要确保pg版本相同,即psql -V输出的结果相同。 导出数据
# 全实例备份(所有库+角色+权限)
pg_dumpall -U postgres -h localhost > pg_all_backup.sql

3. 移动数据文件#

  • 想办法把导出的这个pg_all_backup.sql文件移动到B服务器里面,尽量移动到/tmp文件夹内,这里我移动到了/tmp/pg_all_backup.sql

4. 导入B服务器的PG里#

# 给文件加权限
chmod 644 /tmp/pg_all_backup.sql
# 执行导入
su - postgres -c "psql -d postgres -f /tmp/pg_all_backup.sql"

看到一堆 CREATE、ALTER 就说明正在导入

5. 更改密码#

新的B服务器里面的密码和A服务器里面的密码是一样的.但如果你想改的话,可以执行

su - postgres -c "psql -c \"ALTER USER postgres PASSWORD '你的新密码';\""
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

新服务器开荒
https://www.403bug.top/posts/new-server-setup/
作者
ma
发布于
2025-01-10
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录