开启Tech Note之旅(一)

由 Pi 发布

作为一个技术小白,拥有一个TECH NOTE是成长途中的必经之路。但是不想CSDN等大佬的BLOG面前班门弄斧,就自己另辟蹊径了(反正也没有人看2333)。开篇TECH NOTE就记录一下这个NOTE是怎么搞起来的吧~

需要准备的材料&APP

  • 一只国外VPS小鸡
  • 国内CDN(打算做全站静态)或国外CDN(不行就Cloudflare暴力解决)
  • caddy2(或者也可以用nginx,这里为了方便HTTPS)
  • Typecho(php7.4 + mysql8.0)

服务器配置

我自己用的CENTOS,鉴于搭这种东西没什么可说的,大部分服务器上手就可以直接working,像我的小鸡只需要加一个SWAP就可以正常工作了,所以就不多写了。(其实是我也不知道该写些什么)

PHP安装

首先创建一个名为caddy创建用户组&用户,用于PHP和Caddy v2的权限管理

# groupadd --system caddy
# useradd --system \
    --gid caddy \
    --create-home \
    --home-dir /var/lib/caddy \
    --shell /usr/sbin/nologin \
    --comment "Caddy web server" \
    caddy

安装编译环境

# yum -y install libxml2 libxml2-devel sqlite-devel bzip2 bzip2-devel libcurl libcurl-devel oniguruma oniguruma-devel libxslt libxslt-devel

获取PHP的bin包,并执行编译配置

# wget https://www.php.net/distributions/php-7.4.4.tar.gz
# tar -zvxf php-7.4.4.tar.gz
# cd php-7.4.4
# ./configure --prefix=/usr/local/php --with-curl --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath  --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml

当看到以下文字说明配置成功

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

执行编译

这里请注意内存较小的小鸡记得开启swap,否则编译会出现内存错误
# make && make install

待编译完成后完成php-fpm的配置

# cp php.ini-production /etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

将下列配置保存在/usr/local/php/etc/php-fpm.d/www.conf

; Start a new pool named 'www'
[www]
listen = /usr/local/php/var/run/$pool-php-fpm.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = caddy
listen.group = caddy
listen.mode = 0660
user = caddy
group = caddy
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
pm.status_path = /status
ping.path = /ping
ping.response = pong
request_terminate_timeout = 100
request_slowlog_timeout = 10s
slowlog = /usr/local/php/var/log/$pool.log.slow

然后执行

# /usr/local/php/sbin/php-fpm --fpm-config /usr/local/php/etc/php-fpm.conf -t

test is successful,则创建服务

# vi /usr/lib/systemd/system/php-fpm.service

输入以下内容

[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm --fpm-config /usr/local/php/etc/php-fpm.conf -c /etc
PrivateTmp=true
[Install]
WantedBy=multi-user.target

然后重载服务,设置启动项,启动,查看服务状态

# systemctl daemon-reload
# systemctl enable php-fpm
# systemctl start php-fpm
# systemctl status php-fom

至此php安装完毕

caddy2 安装

caddy具体还是用过的,印象比较深刻的就是轻,然后就是一键HTTPS,其他没有太多印象,因为没有跑过重度服务,据说性能还是略逊于ng。考虑自己的BLOG也没有多少人踩门,所以就用Caddy2看看新特性踩坑玩了,

在我安装的时候caddy2还没有一键bash,所以就自己手动二进制包一步一步来

# yum install wget
# wget https://github.com/caddyserver/caddy/releases/download/v2.0.0-rc.1/caddy_2.0.0-rc.1_Linux_x86_64.tar.gz
# tar -zvxf caddy_2.0.0-rc.1_Linux_x86_64.tar.gz
# mv caddy /usr/bin/

然后试着输入

# caddy version

v2.0.0-rc.1 h1:DxUlg4kMisXwXVnWND7KEPl1f+vjFpIOzYpKpfmwyj8=

如果能看到上面的输出,就说明安装成功了。

配置Caddy

接下来创建网站根目录,并编辑caddy的配置文件,注意将example.com替换为自己的域名

# mkdir /www
# mkdir /www/wwwroot
# mkdir /www/wwwroot/example.com
# chown caddy:caddy /www/wwwroot/example.com
# mkdir /etc/caddy
# touch /etc/caddy/Caddyfile
# vim /etc/caddy/Caddyfile

随后贴入下面的配置文件

example.com

root * /www/wwwroot/example.com
try_files {path} /index.php?{query}&p={path}
php_fastcgi unix//usr/local/php/var/run/www-php-fpm.sock
file_server

接下来下载caddy.service注册caddy为守护进程,放至/etc/systemd/system/caddy.service,在AmbientCapabilities=CAP_NET_BIND_SERVICE后加入一行CapabilityBoundingSet=CAP_NET_BIND_SERVICE,解决非root用户1024以下端口不能绑定问题,重载守护,允许自启,开启!


# wget https://raw.githubusercontent.com/caddyserver/dist/master/init/caddy.service
# mv caddy.service /etc/systemd/system/caddy.service
# systemctl daemon-reload
# systemctl enable caddy
# systemctl start caddy
# systemctl reload caddy

看看运行状态

# systemctl status caddy

至此 Caddy v2 + PHP 已经配置完毕

安装mysql

获取mysql安装文件

# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar
# tar -vxf mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar
# tar yum install mysql*
# systemctl start mysqld
# systemctl enable mysqld
# systemctl daemon-reload

在mysql正常安装后,可以通过下列命令查看mysql安装密码

# systemctl start mysqld
# grep 'temporary password' /var/log/mysqld.log

进入mysql,并输入密码

# mysql -u root -p

然后修改密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';

创建数据库,为后续安装typecho做准备

> create database typecho

至此mysql安装完成

安装typecho

尝鲜就要吃最新鲜,这里安装的是Typecho的测试版,直接获取实时打包版本

# wget http://typecho.org/build.tar.gz
# tar -zvxf build.tar.gz
# cd build
# mv * /www/wwwroot/example.com/
# chown caddy:caddy /www/wwwroot/example.com/ -R

进入安装界面,填写相关信息,数据库地址不要用localhost,写为127.0.0.1即可实现正常的安装!

第二篇,将实现BLOG的全站静态化CDN和装修操作


暂无评论

发表评论