如何安装linux博客
如何安装linux博客详细介绍
在 Linux 系统上搭建博客平台(如 WordPress)通常需要部署 LAMP/LEMP 环境,并完成博客软件的配置。以下是基于 Ubuntu 和 CentOS 系统的详细安装指南:
一、选择 Linux 发行版
推荐使用Ubuntu 22.04 LTS或CentOS 8,两者均提供稳定的环境和完善的社区支持。以下步骤以 Ubuntu 为例,CentOS 用户需替换包管理命令(如apt改为yum或dnf)。
二、部署 LAMP/LEMP 环境
Ubuntu 下 LAMP 安装(Apache + MySQL + PHP)
- 更新系统
bashupdate upgrade - 安装 Apache
bashapache2验证:访问
http://服务器IP,应显示 Apache 默认页面。 - 安装 MySQL
bashmysql-server加固设置:
bashmysql_secure_installation(按提示设置 root 密码、移除匿名用户等)
- 安装 PHP 及扩展
bashphp libapache2-mod-php php-mysql php-curl php-gd php-intl php-mbstring php-xml php-xmlrpc php-soap php-zip验证 PHP:创建测试文件
/var/www/html/info.php,内容为<?php phpinfo(); ?>,访问http://服务器IP/info.php。
CentOS 下 LEMP 安装(Nginx + MariaDB + PHP)
- 安装 Nginx
bashyum epel-releaseyum nginx
systemctl nginx
验证:访问
http://服务器IP,应显示 Nginx 欢迎页面。 - 安装 MariaDB
bashyum mariadb-server mariadbsystemctl mariadb
mysql_secure_installation
- 安装 PHP 及扩展
bashyum php-fpm php-mysqlnd php-curl php-gd php-intl php-mbstring php-xml php-xmlrpc php-soap php-zip配置 Nginx 支持 PHP:
bash/etc/nginx/conf.d/blog.conf添加以下内容:
nginxyour_domain.com
/var/www/html
index.php index.html
/
/ =404
~ \.php$
unix:/var/run/php-fpm/www.sock
index.php
SCRIPT_FILENAME
fastcgi_params
保存后重启服务:
bashsystemctl restart nginx php-fpm
三、安装 WordPress
- 下载 WordPress
bashhttps://wordpress.org/latest.tar.gzlatest.tar.gz /var/www/html/
- 配置文件权限
bashwww-data:www-data /var/www/html/wordpress - 创建数据库和用户
登录 MySQL/MariaDB:
bashmysql root执行以下命令(替换
your_database、your_user和your_password):
sqlyour_databaseIDENTIFIED
your_database
FLUSH
- 配置 WordPress
复制配置文件:
bash/var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php编辑
wp-config.php:
bash/var/www/html/wordpress/wp-config.php找到以下行并替换为数据库信息:
php - 完成安装
访问http://服务器IP/wordpress,按提示完成安装,设置站点标题、管理员账户等。
四、配置 SSL 证书(Let's Encrypt)
- 安装 Certbot
bashcertbot python3-certbot-apacheyum certbot python3-certbot-nginx
- 申请证书
bashcertbot your_domain.comcertbot your_domain.com
按提示输入邮箱并同意条款,证书将自动配置并启用 HTTPS。
- 自动续期
Certbot 默认设置每周自动检查证书状态并续期,无需手动操作。
五、安全加固
- 防火墙设置
bashufw allowfirewall-cmd --add-servicehttp
firewall-cmd --add-servicehttps
firewall-cmd
- SELinux 配置(CentOS)
若遇到权限问题,可临时切换至警告模式:
bashsetenforce永久禁用需编辑
/etc/selinux/config,将SELINUX=enforcing改为SELINUX=permissive。 - 禁用目录索引
编辑 Apache 配置(Ubuntu):
bash/etc/apache2/apache2.conf在
<Directory /var/www/>部分添加Options -Indexes。
六、备份与维护
- 数据库备份
创建备份脚本backup.sh:
bashmysqldump your_user your_database /path/to/backup/ +%F_blog_backup.sql.gz
添加执行权限并设置定时任务:
bash+x backup.sh* * * /path/to/backup.sh
- 文件备份
定期备份 WordPress 目录:
bashwordpress_backup_ +%F.tar.gz /var/www/html/wordpress - 更新软件
bashupdate upgradeyum update
七、国内镜像源优化(可选)
- 更换软件源
Ubuntu:编辑/etc/apt/sources.list,替换为阿里云源:
plaintextdeb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
CentOS:使用
yum-config-manager切换至阿里云源:
bashyum yum-utilsyum-config-manager --add-repo http://mirrors.aliyun.com/repo/Centos-8.repo
- NPM 镜像
配置淘宝镜像:
bashconfig registry https://registry.npm.taobao.org
八、常见问题处理
- PHP 版本不兼容:确保 PHP 版本≥7.4(推荐 8.0+),可通过
php -v检查。 - 数据库连接失败:确认
wp-config.php中的数据库信息正确,检查 MySQL 服务状态。 - SELinux 权限问题:使用
chcon命令修改文件安全上下文或临时禁用 SELinux。
通过以上步骤,您已成功在 Linux 上搭建了一个安全、高效的博客平台。后续可通过 WordPress 后台安装主题和插件,进一步个性化您的博客。