用Nginx + WordPress + Sqlite搭建博客
安装Nginx
# apt install nginx
申请SSL证书
# apt install certbot python2-certbot-nginx
# certbot --nginx
安装PHP
# apt install software-properties-common
# add-apt-repository ppa:ondrej/php
# apt-get update
# apt-get install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-json php7.4-opcache php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl php7.4-sqlite3
修改Nginx配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.myhost.cf myhost.cf;
# Redirect http to https
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name www.myhost.cf myhost.cf; # managed by Certbot
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myhost.cf/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myhost.cf/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ^~ /wordpress/wp-content/database/ {
deny all;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
安装配置WordPress
为确保可以使用Sqlite,不使用最新版本的WordPress,而是使用5.4版。
# cd /var/www/html
# wget https://wordpress.org/wordpress-5.4.4.tar.gz
# tar zxvf wordpress-5.4.4.tar.gz
# wget https://github.com/aaemnnosttv/wp-sqlite-db/archive/v1.0.tar.gz
# tar zxvf v1.0.tar.gz wp-sqlite-db-1.0/src/db.php
# mv wp-sqlite-db-1.0/src/db.php wordpress/wp-content/
# rm -rf wp-sqlite-db-1.0
修改wordpress默认配置文件,以支持sqlite数据库:
# cp wordpress/wp-config-sample.php wordpress/wp-config.php
# nano wordpress/wp-config.php
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', '' );
/** MySQL database password */
define( 'DB_PASSWORD', '' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/** sqlite setting */
define('USE_MYSQL', false);
设置目录权限:
# chown -R www-data:www-data wordpress
最后,创建index.php文件,重定向至wordpress:
<?php
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
$uri = 'https://';
} else {
$uri = 'http://';
}
$uri .= $_SERVER['HTTP_HOST'];
header('Location: '.$uri.'/wordpress/');
exit;
?>