600 字
2 分钟
新服务器开荒
背景介绍
我通过codebuddy新年活动获取了一个免费的CentOS 9服务器
服务器架构配置解析
[root@VM-0-4-centos ~]# echo "=== CPU ===" && lscpu | grep -E "Model name|CPU\(s\)|Thread"echo "=== 内存 ===" && free -hecho "=== 磁盘 ===" && df -hecho "=== 系统 ===" && uname -a=== CPU ===CPU(s): 2On-line CPU(s) list: 0,1Model name: Intel(R) Xeon(R) Platinum 8255C CPU @ 2.50GHzBIOS Model name: 3.0Thread(s) per core: 1NUMA node0 CPU(s): 0,1=== 内存 === total used free shared buff/cache availableMem: 1.7Gi 266Mi 939Mi 6.0Mi 504Mi 1.3GiSwap: 0B 0B 0B=== 磁盘 ===Filesystem Size Used Avail Use% Mounted ondevtmpfs 4.0M 0 4.0M 0% /devtmpfs 856M 24K 856M 1% /dev/shmtmpfs 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 -icp /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 -y2. 启动 & 设置开机自启
# 启动 Nginxsystemctl start nginx
# 设置开机自启systemctl enable nginx
# 查看运行状态systemctl status nginx3. 防火墙放行端口
# 放行 HTTP / HTTPSfirewall-cmd --permanent --add-service=httpfirewall-cmd --permanent --add-service=httpsfirewall-cmd --reload
# 验证firewall-cmd --list-all4. 核心配置文件结构
/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.rpmLast 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:00Dependencies 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 kInstalled size: 16 kDownloading Packages:Running transaction checkTransaction check succeeded.Running transaction testTransaction 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 postgresqlImporting 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-RHELImporting 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-RHELImporting 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-RHELImporting 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-RHELImporting 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-RHELImporting 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 postgresql16Last 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 MInstalling dependencies: postgresql16-libs x86_64 16.13-1PGDG.rhel9.7 pgdg16 335 k
Transaction Summary====================================================================================================================Install 2 Packages
Total download size: 2.1 MInstalled size: 11 MDownloading 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:00PostgreSQL 16 for RHEL / Rocky Linux / AlmaLinux 9 - x86_64 2.4 MB/s | 2.4 kB 00:00Importing 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-RHELKey imported successfullyRunning transaction checkTransaction check succeeded.Running transaction testTransaction 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 -Vpsql (PostgreSQL) 16.13PostgreSQL 数据库服务安装
# 安装 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.confecho "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/16/data/pg_hba.conf
# 2. 重启生效systemctl restart postgresql-16pg 数据库迁移
- 我有个限时的服务器(记作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.sql3. 移动数据文件
- 想办法把导出的这个
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 '你的新密码';\"" 分享
如果这篇文章对你有帮助,欢迎分享给更多人!
部分信息可能已经过时
相关文章 智能推荐





