MalacoWiki

wiki of malacology

Ubuntu环境下搭建博客Typecho

这篇文章介绍了Ubuntu 18.04上如何搭建LNMP环境,并搭建typecho博客服务。

环境

Nginx

apt-get install nginx //apt-get可换成apt,本文所有命令都没有使用super user权限,若需要使用命令前加sudo
apt-get remove apache

PHP

apt-get update && apt-get upgrade
apt-get install software-properties-common //安装储存库
add-apt-repository ppa:ondrej/php //添加php储存库
apt-get update //更新安装包
apt-get install php7.2 //安装php7.2,若安装其他应用7.2更为稳定
service apache2 stop
apt-get install php7.2-mysql php7.2-gd php7.2-fpm php7.2-cgi php7.2-xml php7.2-mbstring php7.2-cur //安装模块

修改/etc/php/7.2/fpm/php.ini
;cgi.fix_pathinfo=1改成cgi.fix_pathinfo=0
修改后重启

操作如下

apt install vim
vim /etc/php/7.2/fpm/php.ini

/后输入查找内容,i进入修改模式,Esc 退出修改模式,:wq保存退出

service php7.2-fpm restart //重启

Mysql

安装

apt-get --purge remove mysql-server mysql-common mysql-client //卸载之前的
apt-get install mysql-server mysql-common mysql-client //安装

设置密码

mysql -u root -p

enter直接进入,修改密码

CREATE DATABASE typecho;
use typecho;
CREATE USER 'typecho'@'localhost' IDENTIFIED BY 'your_password'; //your_password记得更改
GRANT ALL PRIVILEGES ON `typecho`.* TO 'typecho'@'localhost'; //设置任意IP都可以登录
FLUSH PRIVILEGES; 

\cexit 退出后重启mysql

service mysql restart //重启

安装typecho

包内移动到/var/www/html

wget http://typecho.org/downloads/1.1-17.10.30-release.tar.gz
tar zxvf 1.1-17.10.30-release.tar.gz //解压后访问你的域名或者IP
vim /var/www/html/config.inc.php //按照求新建
cd /var/www/html
chmod -R 755 * //修改权限
chown {www-data}/{www-data} * //修改所有者
  • 数据库名和用户名为typecho,密码为自己设置

反向代理

cd /etc/nginx/conf.d
vim default.conf

文件内容为

#此处为反向代理和HTTPS
server {
    listen 443;
    server_name your.domain.net; #不要http://或者https://,只写你的域名
    ssl_certificate /etc/ssl/blog/example.crt; #/etc/ssl/blog/example.crt是你证书的位置(买腾讯域名申请即可免费获得)
    ssl_certificate_key /etc/ssl/blog/example.net.key; #change /etc/ssl/blog/example.net.key是你证书的位置
    ssl on;
    ssl_session_cache builtin:1000 shared:SSL:10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/access.log; #检查一下是否有这个文件
    
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://IP:port; #改成你的ip和域名
        proxy_read_timeout 90;
        proxy_redirect http://IP:port https://your.domain.net; #改成你的ip和域名
    }
}
#强制HTTPS
server {
            listen 80;
            server_name your.domain.net; #改成你的域名
            return 301 https://$server_name$request_uri;
}

重启

service nginx restart

强制SSL

vim /var/www/html/config.inc.php

添加

/** 开启HTTPS */
define('__TYPECHO_SECURE__',true);

/var/www/html文件夹下添.htaccess

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://% {SERVER_NAME}%{REQUEST_URI} [L,R]

How to Cite

Zhang G. 2021. Host Typecho on Ubuntu. malacology.net [Access Time]

Last updated on 22 Apr 2021
Published on 22 Apr 2021

Powered by Arch. Copyright by Guoyi.