今天搭建了一个DHT 磁力搜索引擎,用到两个开源项目:
前端(WEB服务):https://github.com/AlphaReign/www-php
爬虫(后端):https://github.com/AlphaReign/scraper
需要的服务器配置要求比较高,之前试过用 oneprovider 的 4C4G 的独服搭建后跑不起来,今天用了台 4C8G 的VPS才成功了,对 CPU 的要求也很高
搭建后,感觉爬虫效率的确是很高,搜索响应也很快,不过目前爬取到的都是国外的种子,全是英文的……
想要搜索中文资源好像不是很方便
当然,也因为是刚搭建不久,入库的种子还不多的原因
下面记录一下搭建过程。
服务器推荐
搭建这个程序,需要服务器的配置比较高才行,如果只是想搭建来完下,推荐以下的服务商家,注册即免费送余额,适合折腾:
安装组件
yum -y groupinstall 'Development Tools'
yum -y install curl git unzip
安装PHP7.0
使用宝塔面板极速安装
安装完成后,到设置里的禁用函数,把 putenv
删除
安装composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer
安装nodejs/pm2/yarn
curl --silent --location https://rpm.nodesource.com/setup_10.x | bash -
yum -y install nodejs
npm i -g pm2
npm i -g yarn
安装 Elasticsearch
在CentOS 7上安装 Elasticsearch, 推荐从官方 Elasticsearch 存储库安装 rpm 软件包
下载导入存储库的公共密钥:
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
创建文件 /etc/yum.repos.d/elasticsearch.repo
:
vi /etc/yum.repos.d/elasticsearch.repo
写入如下内容:
[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md
保存退出然后安装:
yum install --enablerepo=elasticsearch elasticsearch
安装完成后,设置开机启动
systemctl daemon-reload
systemctl enable elasticsearch.service
启动、停止、查看状态:
systemctl start elasticsearch.service
systemctl restart elasticsearch.service
systemctl stop elasticsearch.service
systemctl status elasticsearch.service
启动后,查看状态,确保 elasticsearch 正常启动
安装MySQL
使用宝塔安装即可,安装后创建数据库,我安装的是5.6
安装前端
拉取前端的源码、安装依赖、修改数据库配置:
cd /opt
git clone https://github.com/AlphaReign/www-php.git
cd www-php
composer install
vi index.php
找到如下位置修改数据库连接配置:
define('DBNAME', 'databasename');
define('DBUSER', 'databaseuser');
define('DBPASS', 'databasepass');
修改:
define('DBNAME', 'dht');
define('DBUSER', 'dht');
define('DBPASS', '你的数据库用户密码');
安装后端
拉取后端的源码、修改数据库配置:
cd /opt
git clone https://github.com/AlphaReign/scraper.git
cd scraper
vi config/index.js
找到如下位置修改数据库连接配置:
client: 'mysql',
connection: {
database: 'alphareign',
host: '127.0.0.1',
password: 'alphareign',
user: 'root',
修改:
client: 'mysql',
connection: {
database: 'dht',
host: '127.0.0.1',
password: '你的数据库用户密码',
user: 'dht',
修改之后:
yarn
yarn migrate
成功:
启动后端服务:
pm2 start ecosystem.config.js
至此所有准备已完成,还差 Nginx 反代
安装 Nginx 反代
宝塔面板安装 nginx
Nginx 的配置:
server {
listen 80;
server_name dht.233.fi; # 换成你的域名
index index.html index.htm index.php;
root /opt/www-php;
client_max_body_size 128g;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
如果是使用宝塔安装,添加站点后,修改站点配置文件:
server
{
listen 80;
server_name pt.uud.me;
index index.html index.htm index.php;
root /opt/www-php;
client_max_body_size 128g;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi-70.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
access_log /www/wwwlogs/pt.uud.me.log;
error_log /www/wwwlogs/pt.uud.me.error.log;
}
注意:如果是宝塔安装的PHP,以上配置文件中,注意将unix:/run/php/php7.0-fpm.sock;
改为 /tmp/php-cgi-70.sock
修改完成之后,打开你绑定的域名,可能会看到一条这样的信息:Doing some maintenance
稍等一下,刷新就能看到登录页面:
注册一个账号,登录后,就可以看到主界面:
左上角数字是当前爬虫到的种子数
这套爬虫效率挺高的,搜索响应也很快,但是对服务器的配置要求比较高
我用的服务器是 4C8G 的,低于4C4G的应该都不需要尝试了……
演示站就不放出来了,加密自用