【docker/宝塔】自己的短链接服务——YOURLS
本文发布于315天前,最后更新于14天前,其中的信息可能有所发展或是发生改变。如有疑问请联系邮箱:admin@yemengstar.com。

1.前言

有自己的短链接服务真的很不错!可以让杂乱无章的链接变得赏心悦目~

官网:YOURLS | YOURLS

GitHub:YOURLS/YOURLS: 🔗 The de facto standard self hosted URL shortener in PHP (github.com)

安装:Installation | YOURLS

2.准备

夜梦这里使用的是雨云的服务器,长期全场景九折 + 首月五折优惠链接:https://www.rainyun.com/

更多服务器推荐请看:服务器推荐 | 部分服务器测评

此项目必须使用域名并开启 SSL。通过 namesilo 注册域名可以使用 1 美元优惠码:yemeng。

在开始部署之前,你需要安装 docker 和 docker-compose。具体安装见:【docker】在服务器上安装 docker/docker-compose

你可以为服务器添加些 swap:手动添加 SWAP

3.部署

3.1使用docker

安装docker和docker-compose:【docker】在服务器上安装docker

创建配置文件(域名一定要改!)

mkdir -p /root/data/docker_data/yourls
cd /root/data/docker_data/yourls
vim docker-compose.yml

根据自己的需求,修改下面的配置文件

version: "3.5"
services:

  mysql:
    image: mysql:5.7 
    environment:
      - MYSQL_ROOT_PASSWORD=my-secret-pw
      - MYSQL_DATABASE=yourls
      - MYSQL_USER=yourls
      - MYSQL_PASSWORD=yourls
    volumes:
      - ./mysql/db/:/var/lib/mysql
      - ./mysql/conf/:/etc/mysql/conf.d
    restart: always
    container_name: mysql
  
  yourls:
    image: yourls
    restart: always
    ports:
      - "8080:80"
    environment:
      YOURLS_DB_HOST: mysql
      YOURLS_DB_USER: yourls
      YOURLS_DB_PASS: yourls
      YOURLS_DB_NAME: yourls
      YOURLS_USER: yemeng      # 自己换一个名字
      YOURLS_PASS: yemeng      # 自己换一个密码
      YOURLS_SITE: https://tech.yemengstar.com  # 换成你自己的域名(站点需要在后面的反向代理中开启SSL)
      YOURLS_HOURS_OFFSET: 8
    volumes:
      - ./yourls_data/:/var/www/html   
    container_name: yourls_service
    links:
      - mysql:mysql

然后就可以启动容器了

docker-compose up -d

此时你还不能访问管理页面,需要我们进行反向代理操作。

3.2使用宝塔

详细部署方式可以看夜梦在雨云论坛发布的教程:使用宝塔部署自己的短链接服务 – 综合讨论 – 雨云论坛

1.下载程序:GitHub-Releases https://github.com/YOURLS/YOURLS/archive/refs/tags/1.9.2.zip

2.宝塔面板中新建一个站点和数据库,将程序上传到站点中。站点需要开启SSL

3.解压程序至站点根目录。

4.将 user/config-sample.php 复制为 user/config.php

5.填写 user/config.php 配置信息。(数据库,站点信息等)

<?php
/* This is a sample config file.
 * Edit this file with your own settings and save it as "config.php"
 *
 * IMPORTANT: edit and save this file as plain ASCII text, using a text editor, for instance TextEdit on Mac OS or
 * Notepad on Windows. Make sure there is no character before the opening <?php at the beginning of this file.
 */

/*
 ** MySQL settings - You can get this info from your web host
 */

/** MySQL database username */
define( 'YOURLS_DB_USER', 'your db user name' );

/** MySQL database password */
define( 'YOURLS_DB_PASS', 'your db password' );

/** The name of the database for YOURLS
 ** Use lower case letters [a-z], digits [0-9] and underscores [_] only */
define( 'YOURLS_DB_NAME', 'yourls' );

/** MySQL hostname.
 ** If using a non standard port, specify it like 'hostname:port', e.g. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', 'localhost' );

/** MySQL tables prefix
 ** YOURLS will create tables using this prefix (eg `yourls_url`, `yourls_options`, ...)
 ** Use lower case letters [a-z], digits [0-9] and underscores [_] only */
define( 'YOURLS_DB_PREFIX', 'yourls_' );

/*
 ** Site options
 */

/** YOURLS installation URL
 ** All lowercase, no trailing slash at the end.
 ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa)
 ** To use an IDN domain (eg http://héhé.com), write its ascii form here (eg http://xn--hh-bjab.com) */
define( 'YOURLS_SITE', 'http://your-own-domain-here.com' );

/** YOURLS language
 ** Change this setting to use a translation file for your language, instead of the default English.
 ** That translation file (a .mo file) must be installed in the user/language directory.
 ** See http://yourls.org/translations for more information */
define( 'YOURLS_LANG', '' );

/** Allow multiple short URLs for a same long URL
 ** Set to true to have only one pair of shortURL/longURL (default YOURLS behavior)
 ** Set to false to allow multiple short URLs pointing to the same long URL (bit.ly behavior) */
define( 'YOURLS_UNIQUE_URLS', true );

/** Private means the Admin area will be protected with login/pass as defined below.
 ** Set to false for public usage (eg on a restricted intranet or for test setups)
 ** Read http://yourls.org/privatepublic for more details if you're unsure */
define( 'YOURLS_PRIVATE', true );

/** A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated
 ** Hint: copy from http://yourls.org/cookie */
define( 'YOURLS_COOKIEKEY', 'modify this text with something random' );

/** Username(s) and password(s) allowed to access the site. Passwords either in plain text or as encrypted hashes
 ** YOURLS will auto encrypt plain text passwords in this file
 ** Read http://yourls.org/userpassword for more information */
$yourls_user_passwords = [
	'username' => 'password',
	// 'username2' => 'password2',
	// You can have one or more 'login'=>'password' lines
];

/** URL shortening method: either 36 or 62
 ** 36: generates all lowercase keywords (ie: 13jkm)
 ** 62: generates mixed case keywords (ie: 13jKm or 13JKm) 
 ** For more information, see https://yourls.org/urlconvert */
define( 'YOURLS_URL_CONVERT', 36 );

/** Debug mode to output some internal information
 ** Default is false for live site. Enable when coding or before submitting a new issue */
define( 'YOURLS_DEBUG', false );

/**
* Reserved keywords (so that generated URLs won't match them)
* Define here negative, unwanted or potentially misleading keywords.
*/
$yourls_reserved_URL = [
	'porn', 'faggot', 'sex', 'nigger', 'fuck', 'cunt', 'dick',
];

/*
 ** Personal settings would go after here.
 */

之后即可访问你的域名进入初始化页面。

PS:用户名密码为自己设置的

$yourls_user_passwords = [
	'yemeng' => 'yemeng',
	// 'username2' => 'password2',
	// You can have one or more 'login'=>'password' lines

伪静态设置(必须!)

location / {
try_files $uri $uri/ /yourls-loader.php?$args;
}

4.反向代理(必须且SSL)

使用docker部署的必须进行反向代理

【docker】反向代理神器——Nginx Proxy Manager

【docker】Nginx Proxy Manager的使用

5.更新与卸载

5.1更新

cd /root/data/docker_data/yourls 
docker-compose pull    
docker-compose up -d   

5.2卸载

cd /root/data/docker_data/yourls
docker-compose down  
cd ~
rm -rf /root/data/docker_data/yourls 

6.使用

完成部署后,你可以访问yourDomain/admin进入管理页面。

点击install yourls进入检查安装界面

检查如下

全部通过即可点击进入管理页面,输入账号与密码

YOURLS_USER: yemeng  #YOURLS用户名
YOURLS_PASS: yemeng  #YOURLS密码

主页面

在enter the URL中,填入你想缩短的域名;在Custom short URL中,填入自定义域名

点击shorten the URL

比如夜梦想要缩短的域名为:https://tech.yemengstar.com/?p=4663

使用的短链接为: https://www.o09uh87.top/yourls

那么访问https://www.o09uh87.top/yourls即可跳转到https://tech.yemengstar.com/?p=4663

还可以查询点击次数

本文为夜梦星尘原创文章。
文章作者:夜梦星尘
文章链接:【docker/宝塔】自己的短链接服务——YOURLS
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自夜梦星尘
支持作者:夜梦星尘的爱发电
上一篇
下一篇