GNU/Linux >> Linux の 問題 >  >> Ubuntu

Ubuntu20.04にNginxを使用してShlinkURLShortenerをインストールする方法

Shlinkは、PHPで記述されたオープンソースのセルフホストURL短縮サービスです。これは、コマンドラインから短いURLを生成および管理するために使用されます。それはあなたが場所、ブラウザまたはリファラーを含むあなたの短いURLのすべての訪問を追跡することを可能にします。 Shlink APIを使用して、サードパーティのツールと統合できます。 URLを短縮するためのコマンドラインインターフェイスを提供します。

このチュートリアルでは、Ubuntu20.04にShlinkをインストールする方法を紹介します。

前提条件
  • Ubuntu20.04を実行しているサーバー。
  • サーバーIPで指定された有効なドメイン名。
  • ルートパスワードはサーバーで構成されています。
はじめに

まず、次のコマンドを実行して、システムパッケージを更新されたバージョンに更新します。

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は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

次に、抽出したディレクトリをNginxWebルートディレクトリに移動します。

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キーを押します。スキーマを選択するように求められます:

 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仮想ホストファイルを作成する必要があります。次のコマンドで作成できます:

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
結論

おめでとう!これで、Ubuntu20.04にShlinkURL短縮サービスが正常にインストールされました。 CLIインターフェースまたはWebベースのクライアントを使用してURLを短縮できるようになりました。


Ubuntu
  1. Ubuntu18.04にNginxでWordPressをインストールする方法

  2. Ubuntu18.04にNginxを使用してphpMyAdminをインストールする方法

  3. Ubuntu16.04にNginxでMediaWikiをインストールする方法

  1. Ubuntu16.04にNginxを使用してGravCMSをインストールする方法

  2. Ubuntu16.04にNginxを使用してSuiteCRMをインストールする方法

  3. Ubuntu15.10にNginxを使用してMagentoをインストールする方法

  1. Ubuntu15.10にNginxでMailpileをインストールする方法

  2. Ubuntu15.10でNginxを使用してRedmine3をインストールする方法

  3. Ubuntu15.10にNginxを使用してPiwikをインストールする方法