轻量级博客搭建

张开发
2026/5/6 8:09:58 15 分钟阅读

分享文章

轻量级博客搭建
0、前言技术栈Nginx 纯 HTML/CSS1.极低资源占用2.极致性能直接返回静态文件没有 PHP/数据库等中间层单机轻松支撑每天几万访问量响应时间通常在 10ms 以内3.安全性最高攻击面最小无动态脚本、无数据库几乎不需要安全更新和维护1、部署系统Ubuntu 22.04.1 LTS1.1 安装依赖rootyyvnjarl:~# apt updaterootyyvnjarl:~# apt install -y make gcc grootyyvnjarl:~# apt install -y libpcre3-dev zlib1g-dev libssl-dev2.2 解压编译nginxrootyyvnjarl:~# tar -xzvf nginx-1.28.2.tar.gz -C /opt/rootyyvnjarl:/opt/nginx-1.28.2# useradd -r -s /sbin/nologin nginxrootyyvnjarl:/opt# cd /opt/nginx-1.28.2/编译./configure --prefix/usr/local/nginx \ --usernginx \ --groupnginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-pcre参数解释1. 安装路径--prefix/usr/local/nginxnginx 最终安装到这里日志、配置、二进制文件都在这。2. 运行用户必须加安全--usernginx --groupnginx让 nginx 以专用用户运行不使用 root更安全。3. HTTPS 支持网站必开--with-http_ssl_module支持 https、证书、SSL。4. 获取真实客户端 IP反向代理必开--with-http_realip_module用在 CDN / 负载均衡后面能拿到用户真实 IP。5. 静态文件压缩优化--with-http_gzip_static_module提前压缩好文件nginx 直接返回性能更高。6. 状态监控页面--with-http_stub_status_module提供 nginx 状态页用于监控连接数、请求数。7. 正则表达式库必开--with-pcre支持 rewrite 重写规则nginx 几乎离不开。编译安装rootyyvnjarl:/opt/nginx-1.28.2# make make installrootyyvnjarl:/usr/local# chown -R nginx:nginx /usr/local/nginx启动rootyyvnjarl:~# /usr/local/nginx/sbin/nginxrootyyvnjarl:~# ss -anplt | grep nginxLISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:((nginx,pid58963,fd6),(nginx,pid58962,fd6))rootyyvnjarl:~# /usr/local/nginx/sbin/nginx -s stoprootyyvnjarl:~# vi /lib/systemd/system/nginx.service加入以下配置[Unit]Descriptionnginx - high performance web serverDocumentationhttp://nginx.org/en/docs/Afternetwork.target remote-fs.target nss-lookup.target[Service]Typeforking# Nginx 启动程序路径ExecStart/usr/local/nginx/sbin/nginx# 重启命令ExecReload/usr/local/nginx/sbin/nginx -s reload# 停止命令ExecStop/usr/local/nginx/sbin/nginx -s quit# 重启策略PrivateTmptrue[Install]WantedBymulti-user.targetrootyyvnjarl:~# systemctl daemon-reloadrootyyvnjarl:~# systemctl enable nginx2.3 安装wordpressrootyyvnjarl:~# apt install php8.1-fpm php8.1-mysql php8.1-xml php8.1-curl \php8.1-mbstring php8.1-zip php8.1-gd php8.1-intl \php8.1-imagick -yrootyyvnjarl:~# apt install mariadb-server wget unzip curl -y配置数据库rootyyvnjarl:~# mysql_secure_installation# 按提示操作# - Enter current password: 回车无密码# - Set root password? [Y/n] Y# - 输入你的数据库 root 密码# - Remove anonymous users? [Y/n] Y# - Disallow root login remotely? [Y/n] Y# - Remove test database and access to it? [Y/n] Y# - Reload privilege tables now? [Y/n] Y创建 WordPress 数据库和用户rootyyvnjarl:~# mysql -u rootWelcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 42Server version: 10.6.23-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type help; or \h for help. Type \c to clear the current input statement.MariaDB [(none)] CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;Query OK, 1 row affected (0.003 sec)MariaDB [(none)] CREATE USER wpuserlocalhost IDENTIFIED BY 你的密码;Query OK, 0 rows affected (0.002 sec)MariaDB [(none)] GRANT ALL PRIVILEGES ON wordpress.* TO wpuserlocalhost;Query OK, 0 rows affected (0.002 sec)MariaDB [(none)] FLUSH PRIVILEGES;Query OK, 0 rows affected (0.001 sec)MariaDB [(none)] exitByerootyyvnjarl:~# cd /var/www/rootyyvnjarl:/var/www# wget https://wordpress.org/latest.tar.gzrootyyvnjarl:/var/www# tar -xzf latest.tar.gzrootyyvnjarl:/var/www# chown -R www-data:www-data /var/www/wordpressrootyyvnjarl:/var/www# chmod -R 755 /var/www/wordpressrootyyvnjarl:/var/www# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bakrootyyvnjarl:/var/www# vi /usr/local/nginx/conf/nginx.confuser www-data; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for; access_log logs/access.log main; error_log logs/error.log; sendfile on; keepalive_timeout 65; # Gzip 压缩 gzip on; gzip_vary on; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/javascript application/xmlrss application/json image/x-icon; # WordPress 站点配置 server { listen 80; server_name www.xxx.com; # 改成你的域名 root /var/www/wordpress; index index.php index.html index.htm; access_log logs/wordpress.access.log; error_log logs/wordpress.error.log; # WordPress 固定链接支持 location / { try_files $uri $uri/ /index.php?$args; } # PHP 处理 location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } # 静态文件缓存 location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf)$ { expires 1y; add_header Cache-Control public, immutable; access_log off; } # 禁止访问敏感文件 location ~ /\. { deny all; } location /wp-config.php { deny all; } location ~ /wp-content/uploads/.*\.php$ { deny all; } } }rootyyvnjarl:/var/www# systemctl start php8.1-fpmrootyyvnjarl:/var/www# systemctl enable php8.1-fpm检查nginx.conf配置文件rootyyvnjarl:/var/www# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful加载配置rootyyvnjarl:/var/www# nginx -s reload检查服务状态rootyyvnjarl:/var/www# systemctl status mariadbrootyyvnjarl:/var/www# systemctl status php8.1-fpmrootyyvnjarl:/var/www# ps aux | grep nginx2.4 配置wordpress访问你的ip端口浏览器访问http://你的ip选择语言- 选择简体中文填写数据库信息数据库名wordpress用户名wpuser密码你设置的密码数据库主机localhost表前缀wp_保持默认运行安装程序设置站点信息站点标题你的博客名称用户名管理员用户名密码设置强密码电子邮件你的邮箱登录 WordPress 后台安装以下插件WP Super Cache或W3 Total Cache- 静态缓存大幅降低资源占用Autoptimize- 优化 CSS/JS

更多文章