IPv6 VPS套了warp后变成了IPv4和IPv6双栈,如何设置IPv4优先呢?
以debain为例,直接修改 /etc/gai.conf 文件:
vi /etc/gai.conf
#precedence ::ffff:0:0/96 100
把前面的”#”去掉即可。
或者:
echo "precedence ::ffff:0:0/96 100" >>/etc/gai.conf
IPv6 VPS套了warp后变成了IPv4和IPv6双栈,如何设置IPv4优先呢?
以debain为例,直接修改 /etc/gai.conf 文件:
vi /etc/gai.conf
#precedence ::ffff:0:0/96 100
把前面的”#”去掉即可。
或者:
echo "precedence ::ffff:0:0/96 100" >>/etc/gai.conf
第一次部署go项目,记录一下过程。
首先update一下:
apt update
apt upgrade
安装nodejs:
apt install nodejs
安装npm:
apt install npm
安装pm2:
npm install pm2 -g
将编译好的go项目文件上传到对应目录之后:
chmod +x golang
运行go项目:
pm2 start golang
查看:
pm2 ls
以debian为例。
创建交换空间文件/swapfile,大小是1G:
fallocate -l 1G /swapfile
默认交换空间文件的权限只有root用户才能写入和读取交换文件的数据。因此我们需要修改交换空间文件的权限为600,并使用mkswap格式化文件。
chmod 600 /swapfile
mkswap /swapfile
启用交换空间:
swapon /swapfile
要在Linux系统启动时自动挂载分区,需要在/etc/fstab文件中定义挂载配置选项:
echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab
运行命令swapon或free命令验证交换空间是否处于活动状态:
swapon --show
free -h
Swappiness
Swappiness是一个Linux内核属性,用于定义系统使用交换空间的频率。Swappiness可以是0到100之间的值。
swappiness=0的时候表示最大限度使用物理内存,然后才是交换空间,swappiness=100的时候表示积极的使用交换空间。
Linux的初始默认设置为60,你可以运行命令cat /proc/sys/vm/swappiness命令查看当前swappiness值的大小。
如果你需要对swappiness的值作出更改,请运行sudo sysctl -w vm.swappiness=10命令。
sysctl命令用于在运行时配置Linux内核的参数,更改仅在当前会话中可用,即重启会恢复为默认值。
为让swappiness的值持久化,则需要将值写入到/etc/sysctl.conf文件中。sysctl.conf是Linux内核的配置文件。在Linux内核启动是将会次配置文件的参数。
运行以下命令持久化Linux内核参数swappiness的值:
echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/sysctl.conf
转载过来备用,原文链接见文章结尾。
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================================#
# System Required: CentOS7 X86_64 #
# Description: FFmpeg Stream Media Server #
# Author: LALA #
# Website: https://www.lala.im #
#=================================================================#
# 颜色选择
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
font="\033[0m"
ffmpeg_install(){
# 安装FFMPEG
read -p "你的机器内是否已经安装过FFmpeg4.x?安装FFmpeg才能正常推流,是否现在安装FFmpeg?(yes/no):" Choose
if [ $Choose = "yes" ];then
yum -y install wget
wget --no-check-certificate https://www.johnvansickle.com/ffmpeg/old-releases/ffmpeg-4.0.3-64bit-static.tar.xz
tar -xJf ffmpeg-4.0.3-64bit-static.tar.xz
cd ffmpeg-4.0.3-64bit-static
mv ffmpeg /usr/bin && mv ffprobe /usr/bin && mv qt-faststart /usr/bin && mv ffmpeg-10bit /usr/bin
fi
if [ $Choose = "no" ]
then
echo -e "${yellow} 你选择不安装FFmpeg,请确定你的机器内已经自行安装过FFmpeg,否则程序无法正常工作! ${font}"
sleep 2
fi
}
stream_start(){
# 定义推流地址和推流码
read -p "输入你的推流地址和推流码(rtmp协议):" rtmp
# 判断用户输入的地址是否合法
if [[ $rtmp =~ "rtmp://" ]];then
echo -e "${green} 推流地址输入正确,程序将进行下一步操作. ${font}"
sleep 2
else
echo -e "${red} 你输入的地址不合法,请重新运行程序并输入! ${font}"
exit 1
fi
# 定义视频存放目录
read -p "输入你的视频存放目录 (格式仅支持mp4,并且要绝对路径,例如/opt/video):" folder
# 判断是否需要添加水印
read -p "是否需要为视频添加水印?水印位置默认在右上方,需要较好CPU支持(yes/no):" watermark
if [ $watermark = "yes" ];then
read -p "输入你的水印图片存放绝对路径,例如/opt/image/watermark.jpg (格式支持jpg/png/bmp):" image
echo -e "${yellow} 添加水印完成,程序将开始推流. ${font}"
# 循环
while true
do
cd $folder
for video in $(ls *.mp4)
do
ffmpeg -re -i "$video" -i "$image" -filter_complex overlay=W-w-5:5 -c:v libx264 -c:a aac -b:a 192k -strict -2 -f flv ${rtmp}
done
done
fi
if [ $watermark = "no" ]
then
echo -e "${yellow} 你选择不添加水印,程序将开始推流. ${font}"
# 循环
while true
do
cd $folder
for video in $(ls *.mp4)
do
ffmpeg -re -i "$video" -c:v copy -c:a aac -b:a 192k -strict -2 -f flv ${rtmp}
done
done
fi
}
# 停止推流
stream_stop(){
screen -S stream -X quit
killall ffmpeg
}
# 开始菜单设置
echo -e "${yellow} CentOS7 X86_64 FFmpeg无人值守循环推流 For LALA.IM ${font}"
echo -e "${red} 请确定此脚本目前是在screen窗口内运行的! ${font}"
echo -e "${green} 1.安装FFmpeg (机器要安装FFmpeg才能正常推流) ${font}"
echo -e "${green} 2.开始无人值守循环推流 ${font}"
echo -e "${green} 3.停止推流 ${font}"
start_menu(){
read -p "请输入数字(1-3),选择你要进行的操作:" num
case "$num" in
1)
ffmpeg_install
;;
2)
stream_start
;;
3)
stream_stop
;;
*)
echo -e "${red} 请输入正确的数字 (1-3) ${font}"
;;
esac
}
# 运行开始菜单
start_menu
继续阅读
以nginx为例:
location /fonts {
# this will echo back the origin header
if ($http_origin ~ "168itw.com$") {
add_header "Access-Control-Allow-Origin" $http_origin;
}
}
或者:
location / {
if ($http_origin ~* "^https?://(168itw.com|www.168itw.com)$") {
add_header Access-Control-Allow-Origin "$http_origin";
}
}
在之前的网站uptime监控中提到过Prometheus+Grafana监控搭配,抽空自己部署了一遍,功能强大,达到了运维级。
Prometheus是主控程序,是监控的Server端。
Node exporter是client端,部署在被监控vps上,用于采集vps数据,向Prometheus传输数据。
Grafana是开源的看板/面板程序,提供可视化图形界面。
三者相结合,提供了一套完整的运维级监控系统。如果需要监控警报,还需要加入Alertmanager。
Prometheus、Node exporter、Altermanager开源地址:
Grafana开源地址:https://github.com/grafana/grafana
下面记录下部署过程。
选择用docker部署,简单方便。注意映射9090端口,及prometheus.yml配置文件目录。我这里以/opt/config目录为例:
docker run \
-p 9090:9090 \
-v /opt/config:/etc/prometheus \
prom/prometheus
将宿主机/opt/config目录,挂载到docker的/etc/prometheus目录。
如果是二进制安装,步骤为:
tar zxf prometheus-2.25.0.linux-amd64.tar.gz -C /opt
mv /opt/prometheus-2.25.0.linux-amd64 /opt/prometheus
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus service
[Service]
User=root
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/opt/prometheus/data
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus
每次更改了Prometheus配置文件,都需要重启:
systemctl restart prometheus
以下为最简单的默认配置文件
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']
labels:
instance: prometheus
添加配置文件之后,docker程序才能正常启动。
此时访问http://ip:9090端口,访问Prometheus控制界面。目前界面很简陋,所以需要继续部署grafana。
以debian为例:
sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
添加源:
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
安装grafana:
sudo apt-get update
sudo apt-get install grafana
启动Grafana:
sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server
配置随机启动:
sudo systemctl enable grafana-server.service
浏览器访问IP:3000端口,即可打开grafana页面,默认用户名密码都是admin,初次登录会要求修改默认的登录密码。
详细安装说明参考grafana官方:https://grafana.com/docs/grafana/latest/setup-grafana/installation/debian/
也可以选择docker安装grafana,更简单方便,参考官方文档:https://grafana.com/grafana/download/11.1.5?edition=enterprise&platform=docker
点击主界面的“Add your first data source”并选择Prometheus,import,保存即可。
当然你还可以添加更多目标,看到更多详细的数据,Grafana官方也提供了很多dashboard的模板:
https://grafana.com/grafana/dashboards/
推荐排名第一的Node Exporter Full。安装模板也很简单,直接复制模板ID(Copy ID to clipboard)
Grafana后台 – Dashboards – Browse – New – Import – 粘贴ID – Load – 并选择数据源为Prometheus。搞定
在需要被监控的vps上部署node exporter,当然也可以在Prometheus所在vps上部署,监控本机。
选择下载对应版本并解压:
wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
tar xvfz node_exporter-1.5.0.linux-amd64.tar.gz
mv /root/node_exporter-1.5.0.linux-amd64 /opt/node_exporter
配置开机启动:
vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter service
[Service]
User=root
ExecStart=/opt/node_exporter/node_exporter
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
如果报错,也可以 vi /etc/systemd/system/node_exporter.service
关于Linux service配置文件的说明,请参考:https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/150.html
systemctl daemon-reload
systemctl enable node_exporter
启动服务:
systemctl start node_exporter
systemctl status node_exporter
node exporter默认监控端口是9100,访问ip:9100可以查看node exporter。
注意:在vps面板防火墙中放行9100端口,同时如果vps安装了宝塔等程序,也需要在”安全“中放行9100端口。
iptables也要放行9100:
iptables -I INPUT -s 0.0.0.0/0 -p tcp --dport 9100 -j ACCEPT
编辑Prometheus的prometheus.yml文件,添加以下代码,注意对齐:
- job_name: 'Linux'
static_configs:
- targets: ['xx.xx.xx.xx:9100']
labels:
instance: 168itw.com
重启docker Prometheus。
继续阅读首先update系统。
安装ffmpeg:
apt-get install ffmpeg
安装streamlink
1、安装pip3
apt-get install python3-pip
2、通过pip安装最新版streamlink
pip3 install --user --upgrade streamlink
3、添加环境变量。
如果是root用户安装,在/root/.bashrc中添加以下代码;
如果是Ubuntu安装,在/home/ubuntu/.bashrc中添加以下代码;
export PATH="${HOME}/.local/bin:${PATH}"
4、重连ssh,查看streamlink是否安装成功:
streamlink -V
1、生成密钥
ssh-keygen
或者:
ssh-keygen -t rsa
一路回车即可。在 root 用户目录中生成了一个 .ssh 目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥。
2、在服务器上安装公钥
[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:
[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh
3、设置 SSH,打开密钥登录功能
编辑 /etc/ssh/sshd_config 文件,进行如下设置(删除这两行前面的注释#):
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
PubkeyAuthentication yes
168itw注:AuthorizedKeysFile不启用,貌似也不影响,没明白这里的作用!
确保允许root 用户通过 SSH 登录:
PermitRootLogin yes
重启ssh服务:
service sshd restart
切换为root用户和密码登录后,下载 .ssh 目录下的私钥文件id_rsa,导入ssh客户端。
确保root用户能够通过密钥登录。
当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:
PasswordAuthentication no
最后,重启 SSH 服务:
[root@host .ssh]$ service sshd restart
继续阅读
Python部署一个项目,运行的时候提示错误:
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-9: ordinal not in range(256)
网上查询说是环境变量问题,python默认编码是 latin1,更改下环境变量。运行以下代码顺利解决。
export PYTHONUTF8=1
顺便记录下Python安装、及过程。
Debian 10安装Python
Debain 10是包换python 2和 python 3的。只需要:
apt update && apt upgrade
查看python版本:
python --version
python3 --version
或者用 python -V查看。
安装pip
apt-get install python3-pip
继续阅读
msql多线程的时候用LAST_INSERT_ID()比max(id)高效且不会被其他线程(connection)打断。
INSERT INTO wp_posts (post_title,post_content,post_author,post_name,post_date,post_excerpt,to_ping,pinged,post_content_filtered) VALUES ('[标签:标题]','[标签:内容]','1','[标签:slug]','[系统时间转化:yyyy-MM-dd HH:mm:ss]','','','','')
INSERT INTO wp_term_relationships (object_id,term_taxonomy_id) VALUES (LAST_INSERT_ID(),1)
但是LAST_INSERT_ID函数有几点必须注意,直接复制过来的:
个人总结的几个注意事项:
1、如果一次insert插入多条value值,LAST_INSERT_ID()获取的是第一条value记录的自增ID;
2、如果是多条insert,插入同一个table表,LAST_INSERT_ID()获取的是最后一条insert记录的自增ID;
3、如果insert的表发生了变化,LAST_INSERT_ID()获取的自增ID也会变化,它返回的是最后一条insert记录的自增ID。
继续阅读