如何在 Ubuntu 22.04 LTS 上安裝 YOURLS

在本教程中,我們將向您展示如何在 Ubuntu 22.04 LTS 上安裝 YOURLS。 對於那些不知道的人,YOURLS 代表您自己的 URL 縮短器,這是一個免費的開源 PHP 腳本,可讓您創建自定義 URL 縮短服務。 它允許您創建簡短的自定義 URL、跟踪點擊統計數據並控制您的數據。

本文假定您至少具有 Linux 的基本知識,知道如何使用 shell,最重要的是,您將站點託管在自己的 VPS 上。 安裝非常簡單,假設您在 root 帳戶下運行,如果不是,您可能需要添加 ‘sudo‘ 到獲取 root 權限的命令。 我將向您展示在 Ubuntu 22.04 (Jammy Jellyfish) 上逐步安裝 YOURLS(您自己的 URL 縮短器)。 您可以按照 Ubuntu 22.04 和任何其他基於 Debian 的發行版(如 Linux Mint、Elementary OS、Pop!_OS 等)的相同說明進行操作。

先決條件

  • 運行以下操作系統之一的服務器:Ubuntu 22.04、20.04 和任何其他基於 Debian 的發行版,如 Linux Mint。
  • 建議您使用全新的操作系統安裝,以防止出現任何潛在問題。
  • 通過 SSH 訪問服務器(如果您在桌面上,則只需打開終端)。
  • 有效的互聯網連接。 您需要互聯網連接才能為 YOURLS 下載必要的軟件包和依賴項。
  • A non-root sudo user或訪問 root user. 我們建議充當 non-root sudo user,但是,如果您在充當根用戶時不小心,可能會損害您的系統。

在 Ubuntu 22.04 LTS Jammy Jellyfish 上安裝 YOURLS

步驟 1. 在我們開始安裝過程之前,最好先更新系統以確保我們擁有最新的軟件包。 為此,請打開終端並運行以下命令:

sudo apt update
sudo apt upgrade
sudo apt install wget apt-transport-https gnupg2

步驟 2. 在 Ubuntu 22.04 上安裝 LAMP Stack。

YOURLS 需要網絡服務器、數據庫和 PHP 才能運行。 如果您沒有安裝 LAMP Stack,您可以按照我們的指南進行操作。

第 3 步。配置 MariaDB。

默認情況下,MariaDB 未加固。 您可以使用以下方法保護 MariaDB mysql_secure_installation 腳本。 您應該仔細閱讀下面的每個步驟,這些步驟將設置 root 密碼、刪除匿名用戶、禁止遠程 root 登錄以及刪除測試數據庫和對安全 MariaDB 的訪問:

mysql_secure_installation

像這樣配置它:

- Set root password? [Y/n] y
- Remove anonymous users? [Y/n] y
- Disallow root login remotely? [Y/n] y
- Remove test database and access to it? [Y/n] y
- Reload privilege tables now? [Y/n] y

接下來,我們需要登錄到 MariaDB 控制台並為 YOURLS 創建一個數據庫。 運行以下命令:

mysql -u root -p

這將提示您輸入密碼,因此請輸入您的 MariaDB root 密碼並點擊 Enter. 登錄到數據庫服務器後,您需要為 Yourls 安裝創建一個數據庫:

MariaDB [(none)]> create database yourlsdb character set utf8mb4;
MariaDB [(none)]> grant all on yourlsdb.* to 'yourls'@'localhost' identified by 'your-strong-passwd';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;

有關安裝 MariaDB 的其他資源,請閱讀以下帖子:

  • 如何在 Ubuntu Linux 上安裝 MariaDB √

步驟 4. 在 Ubuntu 22.04 上安裝 YOURLS。

默認情況下,YOURLS 在 Ubuntu 22.04 基本存儲庫中不可用。 現在運行以下命令從官方網站下載最新版本的 YOURLS:

wget https://github.com/YOURLS/YOURLS/archive/refs/tags/1.9.2.tar.gz

接下來,解壓縮下載的存檔:

sudo tar -xvzf 1.9.2.tar.gz
sudo mv YOURLS-1.9.2/* /var/www/yourls/
sudo rm -rf YOURLS-1.9.2/

之後,將 YOURLS 目錄的所有權更改為 Apache 用戶:

sudo chown -R www-data:www-data /var/www/yourls
sudo chmod -R 755 /var/www/yourls
sudo chmod 777 /var/www/yourls/user/config.php

接下來,複製 user/config-sample.phpuser/config.php:

cp user/config-sample.php user/config.php
nano user/config.php

編輯以下行以匹配您的數據庫配置:

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

/** MySQL database username */
define( 'YOURLS_DB_USER', 'yourls' );

/** MySQL database password */
define( 'YOURLS_DB_PASS', 'your-strong-passwd' );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', 'yourlsdb' );

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

/** MySQL tables prefix */                                                                                                                            
define( 'YOURLS_DB_PREFIX', 'yourls_' );

為 YOURLS 設置網站 URL:

/** YOURLS installation URL -- all lowercase, no trailing slash at the end.
 ** If you define it to "https://sho.rt", don't use "https://www.sho.rt" in your browser (and vice-versa) */
define( 'YOURLS_SITE', 'https://yourls.your-domain.com' );

接下來,設置用戶和密碼:

/** 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 https://yourls.org/userpassword for more information */
$yourls_user_passwords = array(
  'admin' => 'meilana',
  'jmutai' => 'meilana-Strong-Password',
   // You can have one or more 'login'=>'password' lines
   );

Save 和 close 文件。

步驟 5. 配置 Apache 虛擬主機。

現在為 YOURLS 創建虛擬主機配置文件:

nano /etc/apache2/sites-available/yourls.conf

添加以下文件:

<VirtualHost *:80>

ServerName url.unixcop.com
DocumentRoot /var/www/yourls

<Directory /var/www/yourls/>
      Options FollowSymlinks
      AllowOverride All
      Require all granted
 </Directory>

ErrorLog ${APACHE_LOG_DIR}/yourls.your-domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/yourls.your-domain.com_access.log combined

</VirtualHost>

Save 和 close 該文件,然後重新啟動 Apache 以便發生更改:

sudo a2enmod rewrite
sudo a2ensite yourls.conf
sudo systemctl restart apache2

有關安裝和管理的其他資源 Apache,閱讀下面的帖子:

  • 如何安裝 Apache 在 Ubuntu Linux 上 √

第 6 步。保護 YOURLS Let’s Encrypt SSL。

首先,使用以下命令安裝 Certbot 客戶端:

sudo apt install certbot python3-certbot-apache2

接下來,獲取您的 SSL 證書 Let’s Encrypt 通過執行以下步驟:

certbot --apache -d yourls.your-domain.com

Let’s Encrypt 證書的有效期為 90 天,強烈建議在證書過期之前續訂證書。 您可以通過運行以下命令來測試證書的自動續訂:

sudo certbot renew --dry-run

步驟 7. 配置防火牆。

現在我們設置一個簡單的防火牆(UFW) Apache 允許在 HTTP 和 HTTPS 的默認 Web 端口上進行公共訪問:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable

第 8 步。訪問 YOURLS Web 界面。

成功安裝後,現在打開您的 Web 瀏覽器並使用 URL 訪問 YOURLS Web UI https://yourls.your-domain.com. 您將被重定向到以下頁面:

恭喜! 您已成功安裝 YOURLS。 感謝您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系統上安裝 YOURLS(您自己的 URL 縮短器)。 如需更多幫助或有用信息,我們建議您查看 YOURLS網站.