在本页面上

  • 前提条件
  • 开始使用
  • 安装 LEMP 服务器
  • 创建 Shlink 数据库
  • 下载 Shlink
  • 安装 Shlink
  • 为 Shlink 配置 Nginx
  • 结论

Shlink 是一个用 PHP 编写的开源且自托管的 URL 缩短器。它用于从命令行生成和管理短 URL。它允许您跟踪短 URL 的所有访问,包括位置、浏览器或来源。它可以使用 Shlink API 与第三方工具集成。它提供了一个命令行界面来缩短 URL。

在本教程中,我将向您展示如何在 Ubuntu 20.04 上安装 Shlink。

前提条件

  • 一台运行 Ubuntu 20.04 的服务器。
  • 一个指向您服务器 IP 的有效域名。
  • 在服务器上配置了 root 密码。

开始使用

首先,通过运行以下命令将系统软件包更新到最新版本:

apt-get update -y

所有软件包更新完成后,您可以继续下一步。

安装 LEMP 服务器

首先,使用以下命令安装 Nginx、PHP、MariaDB 和其他所需的软件包:

apt-get install nginx mariadb-server php-apcu php7.4 php7.4-fpm php7.4-mysql php7.4-gd php7.4-common php7.4-curl php7.4-intl php7.4-gmp php7.4-xml php-dev php-pear unzip -y

所有软件包安装完成后,您可以继续下一步。

创建 Shlink 数据库

Shlink 使用 MariaDB 作为数据库后端。因此您需要为 Shlink 创建一个数据库和用户。首先,使用以下命令连接到 MariaDB:

mysql

连接后,使用以下命令创建数据库和用户:

MariaDB [(none)]> CREATE DATABASE shlink;
MariaDB [(none)]> GRANT ALL ON shlink.* TO 'shlink'@'localhost' IDENTIFIED BY 'password';

接下来,使用以下命令刷新权限并退出 MariaDB:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

下载 Shlink

首先,从其官方下载页面下载最新版本的 Shlink:

wget https://github.com/shlinkio/shlink/releases/download/v2.7.1/shlink2.7.1_php7.4_dist.zip

下载完成后,使用以下命令解压缩下载的文件:

unzip shlink2.7.1_php7.4_dist.zip

接下来,将解压后的目录移动到 Nginx Web 根目录:

mv shlink2.7.1_php7.4_dist /var/www/html/shlink

接下来,使用以下命令设置适当的权限和所有权:

chown -R www-data:www-data /var/www/html/shlink/
chmod -R 755 /var/www/html/shlink/

安装 Shlink

接下来,运行以下命令以开始安装:

sudo -u www-data php7.4 /var/www/html/shlink/bin/install

系统将要求您选择数据库,如下所示:

 Welcome to Shlink!!
 This tool will guide you through the installation process.

DATABASE
========

 Select database type [MySQL]:
  [0] MySQL
  [1] MariaDB
  [2] PostgreSQL
  [3] MicrosoftSQL
  [4] SQLite
 > 1

选择任意选项并按 Enter。系统将要求您提供数据库名称,如下所示:

 Database name [shlink]:
 > 

提供您的数据库名称或按 Enter 选择默认值。系统将要求您提供数据库主机:

 Database host [localhost]:
 > 

提供您的数据库主机或按 Enter 选择默认值。系统将要求您提供数据库端口:

 Database port [3306]:
 > 

提供您的数据库端口或按 Enter 选择默认值。系统将要求您提供数据库用户名:

 Database username:
 > shlink

提供您的数据库用户名并按 Enter。系统将要求您提供数据库用户密码:

 Database password:
 > password

提供您的数据库用户密码并按 Enter。系统将要求您提供 Unix 套接字。

 Unix socket (leave empty to not use a socket):
 > 

将其留空并按 Enter。系统将要求您提供域名:

URL SHORTENER
=============

 Default domain for generated short URLs:
 > shlink.linuxbuz.com

提供您的域名并按 Enter。系统将要求您选择 Schema:

 Select schema for generated short URLs [http]:
  [0] http
  [1] https
 > 0

选择一个选项并按 Enter。系统将要求您验证长 URL:

 Do you want to validate long urls by 200 HTTP status code on response? (yes/no) [yes]:
 > 

选择默认选项并按 Enter。系统将要求您提供生成的短代码的长度:

 What is the default length you want generated short codes to have? (You will still be able to override this on every created short URL) [5]:
 > 

选择默认值并按 Enter。您应该看到以下输出:

 Do you want Shlink to resolve the short URL title based on the long URL 's title tag (if any)? Otherwise, it will be kept empty unless explicitly provided. (yes/no) [no]:
 > 

选择默认答案并按 Enter。您应该看到以下输出:

 Provide a GeoLite2 license key. (Leave empty to use default one, but it is strongly recommended to get your own. Go to https://shlink.io/documentation/geolite-license-key to know how to get it):
 > 

将其留空并按 Enter。您应该看到以下输出:

 What kind of redirect do you want your short URLs to have? [All visits will always be tracked. Not that good for SEO.]:
  [302] All visits will always be tracked. Not that good for SEO.
  [301] Best option for SEO. Redirect will be cached for a short period of time, making some visits not to be tracked.
 > 301

选择任意选项并按 Enter。您应该看到以下输出:

 How long (in seconds) do you want your redirects to be cached by visitors? [30]:
 > 

选择默认值并按 Enter。您应该看到以下输出:

TRACKING
========

 Do you want track orphan visits? (visits to the base URL, invalid short URLs or other "not found" URLs) (yes/no) [yes]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

 Provide a parameter name that you will be able to use to disable tracking on specific request to short URLs (leave empty and this feature won't be enabled):
 > 

只需按 Enter。您应该看到以下输出:

 Do you want to completely disable visits tracking? (yes/no) [no]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

 Do you want to disable tracking of visitors' IP addresses? (yes/no) [no]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

 Do you want visitors' remote IP addresses to be anonymized before persisting them to the database? (yes/no) [yes]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

 Do you want to disable tracking of visitors' "User Agents"? (yes/no) [no]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

 Do you want to disable tracking of visitors' "Referrers"? (yes/no) [no]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

REDIRECTS
=========

 Custom URL to redirect to when a user hits Shlink's base URL (If no value is provided, the user will see a default "404 not found" page):
 > http://shlink.linuxbuz.com

提供您的域名并按 Enter。您应该看到以下输出:

 Custom URL to redirect to when a user hits an invalid short URL (If no value is provided, the user will see a default "404 not found" page):
 > http://shlink.linuxbuz.com

提供您的域名并按 Enter。您应该看到以下输出:

 Custom URL to redirect to when a user hits a not found URL other than an invalid short URL (If no value is provided, the user will see a default "404 not found" page):
 > http://shlink.linuxbuz.com

提供您的域名并按 Enter。您应该看到以下输出:

APPLICATION
===========

 Do you want to enable a safety check which will not allow short URLs to be deleted after receiving a specific amount of visits? (yes/no) [yes]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

 What is the amount of visits from which the system will not allow short URLs to be deleted? [15]:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

 What is the path from which shlink is going to be served? (Leave empty if you plan to serve shlink from the root of the domain) []:
 > 

选择默认选项并按 Enter。您应该看到以下输出:

INTEGRATIONS
============

 Provide a comma-separated list of redis server URIs which will be used for shared caching purposes under shlink multi-instance contexts (Leave empty if you don't want to use redis cache):
 > 

 Custom configuration properly generated!

Initializing database... Success!
Updating database... Success!
Generating proxies... Success!
Downloading GeoLite2 db file... Success!

                                                                                                                        
 [OK] Installation complete!                                                                                                                                                            

为 Shlink 配置 Nginx

接下来,您需要为 Shlink 创建一个 Nginx 虚拟主机文件。您可以使用以下命令创建它:

nano /etc/nginx/conf.d/shlink.conf

添加以下行:

server {
   listen 80;
   server_name shlink.example.com;

   root /var/www/html/shlink/public;
   error_log /var/log/nginx/shlink.error;
   access_log /var/log/nginx/shlink.access;

   index index.php index.html index.htm index.nginx-debian.html;

   location / {
     # try to serve file directly, fallback to app.php
     try_files $uri /index.php$is_args$args;
   }

   # redirect some entire folders
     rewrite ^/(vendor|translations|build)/.* /index.php break;

   location ~ \.php$ {
     fastcgi_split_path_info ^(.+\.php)(/.+)$;
     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
   }

}

保存并关闭文件,然后使用以下命令验证 Nginx 是否有任何错误:

nginx -t

您应该得到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,重启 Nginx 服务以应用更改:

systemctl restart nginx

您还可以使用以下命令验证 Nginx 的状态:

systemctl status nginx

您应该得到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-06-02 05:28:30 UTC; 4s ago
       Docs: man:nginx(8)
    Process: 25639 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 25653 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 25655 (nginx)
      Tasks: 2 (limit: 2353)
     Memory: 2.6M
     CGroup: /system.slice/nginx.service
             ??25655 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??25656 nginx: worker process

Jun 02 05:28:29 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 02 05:28:30 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.

您现在可以安装设置 Shlink 客户端来缩短 URL。

您还可以从命令行在服务器上生成短 URL:

sudo -u www-data php7.4 /var/www/html/shlink/bin/cli short-url:generate

要列出所有短 URL,请运行以下命令:

sudo -u www-data php7.4 /var/www/html/shlink/bin/cli short-url:list

要显示任何帮助信息,请运行以下命令:

sudo -u www-data php7.4 /var/www/html/shlink/bin/cli

结论

恭喜!您已在 Ubuntu 20.04 上成功安装 Shlink URL 缩短器。您现在可以使用 CLI 界面或基于 Web 的客户端缩短 URL。