优化 CentOS 7 或者 Ubuntu 16.04 以上版本下的 shadowsocks-libev 网络速度,让 shadowsocks-libev 轻松跑满宽带。同样适用于老版本 3.0 的 shadowsocks 优化。

如未安装 shadowsocks-libev 的可乘坐直达航班: CentOS 安装    Ubuntu 安装

一、优化吞吐量

1、新建配置文件:

sudo vi /etc/sysctl.d/local.conf

复制粘贴:

#max open files
fs.file-max = 51200
#max read buffer
net.core.rmem_max = 67108864
#max write buffer
net.core.wmem_max = 67108864
#default read buffer
net.core.rmem_default = 65536
#default write buffer
net.core.wmem_default = 65536
#max processor input queue
net.core.netdev_max_backlog = 4096
#max backlog
net.core.somaxconn = 4096
#resist SYN flood attacks
net.ipv4.tcp_syncookies = 1
#reuse timewait sockets when safe
net.ipv4.tcp_tw_reuse = 1
#turn off fast timewait sockets recycling
net.ipv4.tcp_tw_recycle = 0
#short FIN timeout
net.ipv4.tcp_fin_timeout = 30
#short keepalive time
net.ipv4.tcp_keepalive_time = 1200
#outbound port range
net.ipv4.ip_local_port_range = 10000 65000
#max SYN backlog
net.ipv4.tcp_max_syn_backlog = 4096
#max timewait sockets held by system simultaneously
net.ipv4.tcp_max_tw_buckets = 5000
#turn on TCP Fast Open on both client and server side
net.ipv4.tcp_fastopen = 3
#TCP receive buffer
net.ipv4.tcp_rmem = 4096 87380 67108864
#TCP write buffer
net.ipv4.tcp_wmem = 4096 65536 67108864
#turn on path MTU discovery
net.ipv4.tcp_mtu_probing = 1

net.ipv4.tcp_congestion_control = bbr

2、运行:

sysctl --system

3、编辑配置文件limits.conf

sudo vi /etc/security/limits.conf

在文件结尾添加两行:

* soft nofile 51200
* hard nofile 51200

4、编辑 shadowsocks-libev 服务

# 服务配置文件名称视具体而定
sudo vi /etc/systemd/system/shadowsocks-libev.service

在[Service]之后加入 ExecStartPre=/bin/sh -c ‘ulimit -n 51200’

[Unit]
Description=Shadowsocks-libev Server
After=network.target

[Service]
Type=simple
# 服务配置可能有所不一样,视实际而定
# 在这里加入 ExecStartPre=/bin/sh -c 'ulimit -n 51200'
ExecStartPre=/bin/sh -c 'ulimit -n 51200'
ExecStart=/usr/local/bin/ss-server -c /etc/shadowsocks-libev/config.json -u
Restart=on-abort

[Install]
WantedBy=multi-user.target

5、重新加载 shadowsocks-libev 服务配置

sudo systemctl daemon-reload

6、重启 Shadowsocks-libev 服务

sudo systemctl restart shadowsocks-libev

二、开启TCP Fast Open

TCP Fast Open可以降低Shadowsocks服务器和客户端的延迟。实际上在上一步已经开启了TCP Fast Open,现在只需要在Shadowsocks配置中启用TCP Fast Open。

1、编辑config.json:

sudo vi /etc/shadowsocks-libev/config.json

将 fast_open 的值由 false 修改为 true

{
     "server":"0.0.0.0",
     "server_port":8388,
     "local_port":1080,
     "password":"password",
     "timeout":600,
     "method":"aes-256-cfb",
     /*这里设置 fast_open:true,如果没有则加入*/
     "fast_open": true
 } 

2、重启 shadowsocks-libev 服务:

sudo systemctl restart shadowsocks-libev

三、最后开启 Google BBR

优化到此基本完成。

CentOS 7 源码安装 libmaxminddb 1.3.2 最新版。 libmaxminddb 是 ngx_http_geoip2_module 依赖项之一。

1、下载源码,并解压

wget https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz
tar -xvf libmaxminddb-1.3.2.tar.gz
cd libmaxminddb-1.3.2

备用下载地址:https://down.24kplus.com/linux/libmaxminddb-1.3.2.tar.gz

2、编译安装

./configure --prefix=/usr
make
sudo make install
sudo ldconfig

yum 源里最新的 gcc 版本是 4.8.5,但很多时候安装软件依赖于更高版本的 gcc,这时候只能通过手动源码安装最新版。

使用源码编译安装gcc耗时非常大,请要有耐心等待,耐心等待,耐心等待,重要的事情说三遍。

如果你觉得步麻烦可以使用 gcc一键安装 帮你完成!

1、下载并解压源码

wget https://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz
tar -zxf gcc-9.2.0.tar.gz
cd gcc-9.2.0

备用下载地址:https://down.24kplus.com/linux/gcc-9.2.0.tar.gz

2、编译安装

下载必要软件包

./contrib/download_prerequisites

如果提示 tar (child): lbzip2: Cannot exec: No such file or directory 错误:

yum -y install bzip2

编译安装gcc

./configure --prefix=/usr \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--enable-bootstrap \
--enable-shared \
--enable-threads=posix \
--enable-checking=release \
--with-system-zlib \
--enable-__cxa_atexit \
--disable-libunwind-exceptions \
--enable-gnu-unique-object \
--enable-linker-build-id \
--with-linker-hash-style=gnu \
--enable-languages=c,c++ \
--enable-plugin \
--enable-initfini-array \
--disable-libgcj \
--enable-gnu-indirect-function \
--with-tune=generic \
--with-arch_32=x86-64 \
--build=x86_64-redhat-linux \
--disable-multilib
make
make install

如果出现 configure: error: GNAT is required to build ada 错误

yum -y install gcc-gnat -y

如果出现 fatal error: zlib.h: No such file or directory 错误

yum install zlib-devel -y

如果出现 configure: error: C++ preprocessor "/lib/cpp" fails sanity check 错误

yum install gcc-c++ -y

如果出现 makeinfo: command not found 错误

yum install texinfo -y

为了避免日后编译软件发生 checking for C compiler … not found 错误,执行:

ln -s /usr/bin/gcc /usr/bin/cc

4、检查是否正确安装

gcc -v

CentOS 7 源码安装 libzip 1.5.2,解决 error: Please reinstall the libzip distributioerror: system libzip must be upgraded to version >= 0.11 错误。

1、准备编译环境

yum install openssl-devel bzip2 bzip2-devel

2、下载源码,并解压

wget https://libzip.org/download/libzip-1.5.2.tar.gz
tar -zxf libzip-1.5.2.tar.gz
cd libzip-1.5.2

备用下载地址:https://down.24kplus.com/linux/libzip-1.5.2.tar.gz

3、编译安装

mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX=/usr .. && make
sudo make install

如果提示 cmake: command not found,需要先安装 cmake

CentOS 7 源码安装 CMake 3.15.5 最新稳定版。解决 cmake: command not found 问题。

1、准备编译环境

yum -y install gcc gcc-c++

2、获取源码,并解压

wget https://github.com/Kitware/CMake/releases/download/v3.15.5/cmake-3.15.5.tar.gz
tar -zxf cmake-3.15.5.tar.gz
cd cmake-3.15.5

备用下载地址:https://down.24kplus.com/linux/cmake/cmake-3.15.5.tar.gz

3、编译安装

./bootstrap --prefix=/usr --datadir=share/cmake --docdir=doc/cmake && make
sudo make install

4、检查是否正确安装

cmake --version

cmake version 3.15.5

CMake suite maintained and supported by Kitware (kitware.com/cmake).

CentOS 7 通过源码安装 re2c 最新版,解决 configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers 问题。

1、准备编译环境

yum -y install git automake gcc+ gcc-c++ libtool

2、获取源码、并编译安装

git clone https://github.com/skvadrik/re2c.git re2c
cd re2c
mkdir -p m4
./autogen.sh && ./configure --prefix=/usr && make
sudo make install

备用下载地址: https://down.24kplus.com/linux/re2c-1.1.1.tar.gz

3、检查是否安装成功

re2c -v

re2c 1.1.1

官方安装文档:http://re2c.org/install/install.html

1、 查看当前sql-mode

select @@sql_mode;
MySQL @@sql_mode

2、设置 sql-mode

拷贝原来的值,移除 NO_ZERO_IN_DATENO_ZERO_DATE 两项即可

SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

3、使新的 sql-mode 生效

FLUSH PRIVILEGES;

1、准备安装环境

sudo apt-get install python python-twisted python-openssl python-setuptools intltool python-xdg python-chardet geoip-database python-libtorrent python-notify python-pygame python-glade2 librsvg2-common xdg-utils python-mako python-dev

2、获取源码并安装

(1)获取源码

git clone git://deluge-torrent.org/deluge.git
cd deluge
git checkout -b master remotes/origin/master

(2)安装

python setup.py build
sudo python setup.py install --install-layout=deb

如果出现 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 错误,执行:

sudo apt-get install python-dev  \
     build-essential libssl-dev libffi-dev \
     libxml2-dev libxslt1-dev zlib1g-dev \
     python-pip

3、配置 deluge 和 deluge-web 服务

sudo cp packaging/systemd/deluged.service /etc/systemd/system/deluged.service
sudo cp packaging/systemd/deluge-web.service /etc/systemd/system/deluge-web.service

4、启动 deluged 和 deluge-web 服务,并设置自启动

sudo systemctl start deluged
sudo systemctl enable deluged

sudo systemctl start deluge-web
sudo systemctl enable deluge-web

5、开启远程访问(可选)

deluge-web默认只允许本地访问。默认端口:8112,默认密码:deluge

#启用远程访问
deluge-console "config -s allow_remote True"

#禁用远程访问
deluge-console "config allow_remote"

访问地址:http://你的IP:8112

deluge web

卸载 Deluge

sudo systemctl stop deluge-web
sudo systemctl disable deluge-web
sudo systemctl stop deluged
sudo systemctl disable deluged
sudo rm -rf /etc/systemd/system/deluge*

sudo rm -rf /usr/bin/deluge*

sudo rm -rf /usr/lib/python2.7/dist-packages/deluge*