ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

搭建Typecho博客

2022-01-14 15:32:01  阅读:215  来源: 互联网

标签:index www 博客 server ssl Typecho apt php 搭建


前言

最开始我用wordpress搭建了博客,因为它的主题、插件非常丰富,功能强大,并且更新活跃。因为我买的便宜的阿里云主机只有1核2G,带宽1M,安装上后发现,单纯浏览博客还行,但是登录后台就非常卡顿。现在对PHP不了解,也没时间去研究,于是我决心换用很轻量的Typecho,最近几个月它又有了更新,所以选择pre-release的1.2版本。

环境配置

apt先更新下。

apt update && apt upgrade

sqlite3

这个博客就是用来记录下学习的琐碎内容,估计也不会有什么流量,所以数据库选最轻量的splite3绰绰有余。

apt install sqlite3

PHP

Typecho 1.2默认可以在php8的环境下运行,如果安装php8的话:

apt install software-properties-common
add-apt-repository ppa:ondrej/php
apt update

apt install php8.0-fpm php8.0-sqlite3 php8.0-mbstring php8.0-curl

目前很多开源主题和插件在php8下可能有更多bug,用php7更妥当,如果之前添加过PPA源就先删掉,再安装php7.4:

# 如果添加过
add-apt-repository -r ppa:ondrej/php && apt update

apt install php-fpm php-sqlite3 php-mbstring php-curl

Nginx

直接apt安装nginx

apt install nginx

然后为博客站点编辑配置文件,Ubuntu里在/etc/nginx/sites-available/default , 不用default也可以生成 新的文件,CentOS的在/etc/nginx/conf.d/

还没域名和SSL证书前,测试可用:

server {
	listen 80 default_server;
  listen [::]:80 default_server;

  root /var/www/html;

  # Add index.php to the list if you are using PHP
  index index.php;

  server_name _;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }

  # pass PHP scripts to FastCGI server
  #
  location ~ \.php(\/.*)*$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php-fpm.sock;
  }

  if (-f $request_filename/index.html){
  	rewrite (.*) $1/index.html break;
  }
  if (-f $request_filename/index.php){
  	rewrite (.*) $1/index.php;
  }
  if (!-e $request_filename){
  	rewrite (.*) /index.php;
  }
}

购买了域名(国内服务器记得ICP备案和公安备案)以及免费的SSL证书,修改nginx配置

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name  yczheng.top;

    ssl_certificate /etc/nginx/ssl/yczheng.top.pem;
    ssl_certificate_key /etc/nginx/ssl/yczheng.top.key;

    root /var/www/html;
    index index.php;

    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php(\/.*)*$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php-fpm.sock;
    }
    
    if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
    }
    if (!-e $request_filename){
            rewrite (.*) /index.php;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name www.yczheng.top;

    ssl_certificate /etc/nginx/ssl/yczheng.top.pem;
    ssl_certificate_key /etc/nginx/ssl/yczheng.top.key;

    return 301 https://yczheng.top$request_uri;
}

server {
    listen 80;
    listen [::]:80;

    server_name www.yczheng.top yczheng.top;

    return 301 https://yczheng.top$request_uri;
}

安裝Typecho

为了不因为权限问题导致网站无法运行,这里我将网站放置在目录/var/www/html里 ,php-fpm默认是www-data用户运行,记得给此用户赋予网站的读写权限(或者把网站文件所属赋予www-data)。

cd ~
# Download pre-release or latest from github
wget https://github.com/typecho/typecho/releases/download/v1.2.0-rc.1/typecho.zip
mv ~/typecho.zip /var/www/html/

cd /var/www/html/
unzip typecho.zip
rm typecho.zip

# php-fpm默认是www-data用户运行
chown -R www-data:www-data /var/www/html/*

然后在本地的浏览器,输入服务器的ip,或者域名(如果配置好),就按照提示一步一步进行就好。

Typecho主题

虽然不如Wordpress,但Typecho仍然有不少主题和插件,在https://typecho.me/能找到很多不错的主题和插件。

我喜欢的一些主题:

参考

https://spinupwp.com/hosting-wordpress-yourself-setting-up-sites/

https://linuxize.com/post/how-to-install-php-8-on-ubuntu-20-04/#installing-php-80-with-nginx

https://learnku.com/php/t/51997

https://juejin.cn/post/6969113070346403853

https://developer.aliyun.com/article/764024

除此之外,很多Wordpress的建站教程也适用。

标签:index,www,博客,server,ssl,Typecho,apt,php,搭建
来源: https://www.cnblogs.com/yczheng/p/setup-typecho.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有