使用基于 PHP 的开源软件 https://github.com/YOURLS/YOURLS 搭建
系统配置 php7.1+mysql5.7+nginx
下载源代码
- Git clone https://github.com/YOURLS/YOURLS.git
- cd YOURLS
- mv user/config-sample.PHP user/config.PHP
修改 config.PHP 里面的配置参数, 依据自己的环境自行修改
- define( 'YOURLS_DB_USER', 'root' );
- define( 'YOURLS_DB_PASS', '123456' );
- define( 'YOURLS_DB_NAME', 'yourls' );
- define( 'YOURLS_DB_HOST', 'localhost' );
- define( 'YOURLS_DB_PREFIX', 'yourls_' );
- // 上面是数据信息不用多说
- define( 'YOURLS_SITE', 'http://test.com' ); // 你自己服务器的域名 用最短的, 短地址也是基于这个生成.
- define( 'YOURLS_HOURS_OFFSET', '+8'); // 时区偏移
- define( 'YOURLS_LANG', 'zh_CN' ); // 这个语言默认是英文, 没有中文包, 需要自己去 https://github.com/guox/yourls-zh_CN / 下载, 放到 user/languages 里面
- define( 'YOURLS_UNIQUE_URLS', true ); // 短地址是否唯一
- define( 'YOURLS_PRIVATE', true ); // 是否私有, 如果私有的, 则进行 API 调用生成短地址时需要传递用户名和密码
- define( 'YOURLS_COOKIEKEY', 'A2C7&H~r80pTps{nIfI8VFpTxnfF3c)[email protected]#{nDUh' );// 加密 cookie 去 http://yourls.org/cookie 获取
- $yourls_user_passwords = array(
- 'admin' => '123456' /* Password encrypted by YOURLS */ , // 用户名 => 密码 可填多个 登录成功后这里的明文密码会被加密
- );
- define( 'YOURLS_DEBUG', false ); // 是否开启调试
- define( 'YOURLS_URL_CONVERT', 62 ); // 使用 36 进制 还是 62 进制 这个最好一开始设好不要修改, 避免地址冲突, 建议 62 进制
- $yourls_reserved_URL = array(
- 'porn', 'faggot', 'sex', 'nigger', 'fuck', 'cunt', 'dick', // 排除一下短地址, 这些地址是不会生成的
- );
配置 nginx, 这里域名以 test.com 为例
- server {
- listen 80;
- server_name test.com;
- root /www/YOURLS/;
- index index.PHP;
- location / {
- try_files $uri $uri/ /yourls-loader.PHP$is_args$args;
- }
- location ~ \.PHP$ {
- root /www/YOURLS/;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.PHP;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
重启 nginx
访问 http://test.com/admin 选择安装 会提示安装成功
短地址默认是是按照顺序生成, 从 1 开始, 有可能我们想不按照这样顺序, 可以推荐用时间戳来生成
只需要修改 includes/functions.PHP 272 行左右
将 $id = yourls_get_next_decimal(); 改为 $id = time();
API 接口生成
请求地址: http:// 域名 //yourls-API.PHP
参数: username(用户名),password(密码),format(格式 JSON),url(长地址),action(功能, shorturl)
返回:
- {
- "url": {
- "keyword": "1H7fa7",
- "url": "http://www.baidu.com/download/index.html?player_id=2",
- "title": "下载页面",
- "date": "2019-03-22 08:04:23",
- "ip": "127.0.0.1"
- },
- "status": "success",
- "message": "http://www.baidu.com/download/index.html?player_id=2 已保存为",
- "title": "下载页面",
- "shorturl": "http://test.com/1H7fa7",
- "statusCode": 200
- }
上面的 shorturl 就是生成的短链接, 也可以在后台直接生成指定的短链接.
每个短链接后面拼接一个 + , 就可以查看这个链接的点击情况, 例如: http://test.com/1H7fa7+
shorturl
来源: http://www.bubuko.com/infodetail-3027713.html