纪念小站成立
2022-10-09折腾了好久,因为不喜欢ubuntu所以选择了更加优雅的ArchLinux,因此好多的教程都无法使用了。另一方面不想使用现有的集成化工具,感觉有太多的我用不到的功能就放弃了集成好的面板工具。所以选择就只有自己造轮子这一条路了,其中走了好多弯路,感觉在能出错的地方就都出错了。好在最后成功了,在这里记录下走过的弯路。
用的Vultr的VPS,之前一直在用Ubuntu的镜像,这次终于有些空闲时间,所以看到新出的ArchLinux的选项之后,我的脑海里就浮现了那个在实验室我一直在用的老旧一体机上的自己用openbox配置的桌面环境。于是,折腾的灵魂再次开始躁动了。
系统设置
首先是系统层面的设置。部署好了Arch之后第一步自然是AUR了。发现自己以前经常用的yaourt已经是昨日黄花了,就去试图物色一个替代品。最终yay凭借它傲人的长度成功晋级。想用AUR自然就不能使用root账号操作了,新建sudo用户,切换成常用的zsh,安装好OhMyZsh一气呵成。下来就是编译yay。
cd /opt
sudo git clone https://aur.archlinux.org/yay.git
sudo chown -R 用户名:users ./yay
cd yay
makepkg -si
当然还要设置防火墙。nftables是下一代的iptables,因此既然要折腾就折腾到底!
sudo systemctl start nftables.service
sudo systemctl enable nftables.service
sudo nft flush ruleset #清除所有现有规则
sudo nft add table inet wall #自定义一个名为wall的表, inet就同时作用于v4和v6
sudo nft add chain inet wall input \{ type filter hook input priority filter \; policy drop \; \} #定义默认链input,默认drop
sudo nft add rule inet wall input ct state established,related accept #已经建立的连接当然要放行了
sudo nft add rule inet wall input iif lo accept #本地连接放行没说的
sudo nft add rule inet wall input ct state invalid drop #无效的连接就扔了吧
sudo nft add rule inet wall input tcp dport \{ssh, https, 8020-8030\} accept #我们要用的端口ssh, https开放一下, 8020-8030是给被动模式的FTP预留的
sudo nft add rule inet wall input meta l4proto ipv6-icmp accept #允许v6的icmp,就可以ping通了
sudo nft add rule inet wall input meta l4proto icmp accept #允许v4的icmp,就可以ping通了
sudo nft add chain inet wall forward \{ type filter hook forward priority filter \; policy drop \; \} #我们坚决不转发,不做路由器
sudo nft add chain inet wall output \{ type filter hook output priority filter \; policy accept \; \} #自己发起的链接放行没说的
nft果然比iptables清爽太多,想要这些设置开机启动只需要将目前的配置写入/etc/nftables.conf就好,而sudo nft -s list ruleset的结果就可以用来写入文件!极度舒适!
HTTPS证书
使用了Let'sEncrypt的免费证书,顺便添加以下计划任务。这里还是每次更新的时候停止了nginx服务的,要是不开放80端口(http)就没必要了。
sudo systemctl stop nginx.service
sudo certbot certonly --standalone -d web.jemirynet.xyz -d web6.jemirynet.xyz -d jemirynet.xyz
sudo sh -c 'printf "#!/bin/sh\systemctl stop nginx.service\n" > /etc/letsencrypt/renewal-hooks/pre/nginx.sh'
sudo sh -c 'printf "#!/bin/sh\systemctl start nginx.service\n" > /etc/letsencrypt/renewal-hooks/post/nginx.sh'
sudo chmod 755 /etc/letsencrypt/renewal-hooks/pre/nginx.sh
sudo chmod 755 /etc/letsencrypt/renewal-hooks/post/nginx.sh
sudo SLEEPTIME=$(awk 'BEGIN{srand(); print int(rand()*(3600+1))}'); echo "0 0,12 * * * root sleep $SLEEPTIME && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null
LNMP安装
就装一下NMP三件套呗。
配置nginx
yay -S nginx-mainline
sudo nano /etc/nginx/nginx.conf
关键配置部分如下
#http转到https,已放弃因为改用了cloudfare实现这一点。重复的配置会导致重定向次数过多
#server {
# listen 80 default_server;
# server_name _;
# return 301 https://$host$request_uri;
# }
server {
listen 127.0.0.1:8080 proxy_protocol;
listen 127.0.0.1:8081 http2 proxy_protocol;
server_name localhost;
add_header Content-Security-Policy "upgrade-insecure-requests"; #回落时候保持样式不变
location / {
root /usr/share/nginx/html;
index index.php; #Wordpress 主页是动态的index.php
#下面是实现伪静态,即把静态的网址解析成参数传给动态主页index.php。这样对搜索引擎友好,更容易被搜索到,可以提高网站知名度(假装)
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后就开启nginx就好了
sudo systemctl start nginx
sudo systemctl enable nginx
安装配置php
为了给Wordpress所有可选依赖都给满足了就都给装了
yay -S php-fpm php php-imagick php-gd php-intl
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo systemctl edit php-fpm.service
最后一条是关键,ArchLinux下默认php创建的文件都是只读的,这会导致后面Wordpress没有权限写操作。(这一点我找了两天才在Archwiki上的一个角落里看到了:sob:)。systemctl edit会自动生成一个override.conf,覆盖系统的system service的行为,关键是要给写权限,就是添加
[Service]
ReadWritePaths=/usr/share/nginx/html
然后把安装的插件都给启用了,主要在文件/etc/php/php.ini里面把exif,gd,iconv,intl,mysqli,pdo_mysql都取消注释(去掉前面的;)。还有/etc/php/conf.d/imagick.ini里的imagick。之后就sudo systemctl restart php-fpm就:ok:了。
接下来是MySql装了
Arch下好像推荐yay -S mariadb。
配置的话主要就是
sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
sudo systemctl start mysqld
sudo systemclt enable mysqld
sudo systemctl status mysqld
sudo mysql_secure_installation
sudo mysql -u root -p
之后就给Wordpress新建个数据库和用户
creat WordPress;
use mysql;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON WordPress.* TO 'wp'@'localhost' IDENTIFIED BY '密码';
分号可不能忘了。
终于到Wordpress了
随便找个正常目录,直接
usermod -G http 用户名 #把自己加到http里面方便线下管理文件
wget https://wordpress.org/latest.zip
unzip latest.zip
rm latest.zip
sudo mv wordpress/* /usr/share/nginx/html
rm -rf wordpress
sudo chown -R 用户名:http /usr/share/nginx/html
sudo chmod 775 /usr/share/nginx/html
cd /usr/share/nginx/html
之后为了不用建FTP就可以在设置文件里添加
define("CONCATENATE_SCRIPTS", false);
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
define("WP_HOME", "http://web.jemirynet.xyz");
define("WP_SITEURL", "http://web.jemirynet.xyz");
另顺手建个FTP
虽说最后成功实现了不用ftp就可以更新Wordpress,但是我当时还是在FTP上摸索了很久的。这里顺手建一下。
yay -S vsftpd
sudo nano /etc/vsftpd.conf
关键要设置的是
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=000
# 巨坑,血泪教训
seccomp_sandbox=NO
isolate_network=NO
ssl_enable=NO
force_local_logins_ssl=NO
force_local_data_ssl=NO
rsa_cert_file=/etc/letsencrypt/live/web.jemirynet.xyz/cert.pem
rsa_private_key_file=/etc/letsencrypt/live/web.jemirynet.xyz/privkey.pem
require_ssl_reuse=NO
listen_port=8021
ftp_data_port=8020
pasv_enable=YES
pasv_min_port=8022
pasv_max_port=8030
local_root=/usr/share/nginx/html
chroot_local_user=NO
allow_writeable_chroot=YES
这里用了被动模式,为了方便客户端连接,不用开放高位端口。之后开启一发
sudo systemctl enable vsftpd.service
sudo systemctl start vsftpd.service
CDN
为了提高网站连接质量,配置一下CDN。用的是Cloudflare的免费服务。在NameCheap的面板上将DNS解析服务器设置成Cloudflare的,按照官方说明 来就好。然后在Cloudflare里面配置好SSL/TLS。这里千万不能在小站上开启http自动跳转https会和Cloudflare的服务冲突,导致报错重定向次数过多。切记!
BBR
BBR是google开发的TCP拥堵控制算法,较新的linux内核默认支持,不过没有开启。在Arch下开启命令如下
sudo su
echo "net.core.default_qdisc=fq" >> /etc/sysctl.d/bbr.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.d/bbr.conf
modprobe tcp_bbr
sysctl -p /etc/sysctl.d/bbr.conf
然后通过下面的命令检查一下
sysctl net.ipv4.tcp_available_congestion_control
sysctl net.ipv4.tcp_congestion_control
lsmod | grep bbr
应该都有bbr字样的输出。
据说BBR开启之后速度会快很多,但是我没有感受到,可能是因为我的网速其实是受限于我的网络环境而不是VPS的吧:joy:
收工!:laughing:
Good post. I learn something totally new and challenging on websites I stumbleupon every day. Its always exciting to read articles from other writers and practice a little something from other sites.
Good post. I learn something new and challenging on blogs I stumbleupon on a daily basis. Its always exciting to read content from other authors and use a little something from other web sites.
Itís nearly impossible to find educated people in this particular subject, however, you seem like you know what youíre talking about! Thanks