웹사이트 검색

Ubuntu 20.04에서 Nginx로 Shlink URL Shortener를 설치하는 방법


이 페이지에서

  1. 전제 조건
  2. 시작하기\n
  3. LEMP 서버 설치
  4. Shlink 데이터베이스 생성
  5. Shlink 다운로드
  6. Shlink 설치
  7. Shlink용 Nginx 구성
  8. 결론

Shlink는 PHP로 작성된 오픈 소스 및 자체 호스팅 URL 단축기입니다. 명령줄에서 짧은 URL을 생성하고 관리하는 데 사용됩니다. 위치, 브라우저 또는 리퍼러를 포함하여 짧은 URL의 모든 방문을 추적할 수 있습니다. Shlink API를 사용하여 타사 도구와 통합할 수 있습니다. URL 단축을 위한 명령줄 인터페이스를 제공합니다.

이 튜토리얼에서는 Ubuntu 20.04에 Shlink를 설치하는 방법을 보여줍니다.

전제 조건

  • Ubuntu 20.04를 실행하는 서버.\n
  • 서버 IP를 가리키는 유효한 도메인 이름입니다.\n
  • 루트 암호는 서버에서 구성됩니다.\n

시작하기

먼저 다음 명령을 실행하여 시스템 패키지를 업데이트된 버전으로 업데이트합니다.

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를 다운로드합니다.

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 웹 루트 디렉터리로 이동합니다.

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 구성

다음으로 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 ubunt4 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 02 05:28:30 ubunt4 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 인터페이스 또는 웹 기반 클라이언트를 사용하여 짧은 URL을 사용할 수 있습니다.