在本页

  • 先决条件
  • 入门
  • 安装 Apache、MariaDB 和 PHP
  • 为 Polr 创建数据库
  • 安装 Composer
  • 安装 Polr
  • 为 Polr 配置 Apache
  • 访问 Polr Web 界面
  • 使用 Let's Encrypt 免费 SSL 保护 Polr
  • 结论

Polr 是一个用 PHP 编写的免费开源 URL 缩短器。它提供了一个简单且用户友好的界面来创建和管理链接。您可以托管自己的 URL 缩短器,为您的 URL 打造品牌,并掌控您的数据。它提供了大量功能,包括强大的 API、URL 转发、自定义权限、主题等。

在本文中,我们将向您展示如何在 Ubuntu 20.04 上使用 Apache 和 Let's Encrypt SSL 安装 Polr。

先决条件

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

入门

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

apt-get update -y

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

安装 Apache、MariaDB 和 PHP

首先,您需要在服务器上安装 Apache 服务器、MariaDB 数据库、PHP 和其他 PHP 扩展。您可以使用以下命令安装所有这些:

apt-get install apache2 mariadb-server php libapache2-mod-php libapache2-mod-wsgi php-gd php-curl php-xml php-xmlrpc php-curl php-imagick php-mbstring php-zip php-intl -y

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

为 Polr 创建数据库

Polr 使用 MariaDB/MySQL 作为数据库后端。因此您需要为 Polr 创建一个数据库和用户。

首先,使用以下命令登录 MariaDB:

mysql

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

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

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

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

完成后,您可以继续下一步。

安装 Composer

接下来,您需要在服务器上安装 Composer,以安装 Polr 所需的 PHP 依赖项。

运行以下命令安装 Composer:

curl -sS https://getcomposer.org/installer | php

您应该得到以下输出:

All settings correct for using Composer
Downloading...

Composer (version 2.1.3) successfully installed to: /root/composer.phar
Use it: php composer.phar

接下来,使用以下命令将 Composer 二进制文件移动到系统位置:

mv composer.phar /usr/local/bin/composer

接下来,使用以下命令验证 Composer 版本:

composer -V

您应该看到以下输出:

Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Composer version 2.1.3 2021-06-09 16:31:20

安装 Polr

接下来,您需要从 Git 仓库下载最新版本的 Polr。您可以使用以下命令下载:

git clone https://github.com/cydrobolt/polr.git --depth=1 /var/www/html/polr

下载完成后,切换目录到 Polr,并使用以下命令安装所有必需的依赖项:

cd /var/www/html/polr
composer install --no-dev -o

接下来,使用以下命令复制示例环境文件:

cp .env.setup .env

接下来,使用以下命令为 Polr 目录设置适当的权限:

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

完成后,您可以继续下一步。

为 Polr 配置 Apache

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

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

添加以下行:

<VirtualHost *:80>
ServerAdmin 
[email protected]

DocumentRoot /var/www/html/polr/public
ServerName polr.example.com
<Directory /var/www/html/polr/public/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/polr-error_log
CustomLog /var/log/apache2/polr-access_log common
</VirtualHost>

保存并关闭文件,然后使用以下命令启用 Apache 虚拟主机文件和 rewrite 模块:

a2enmod rewrite
a2ensite polr.conf

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

systemctl restart apache2

您现在可以使用以下命令验证 Apache 服务的状态:

systemctl status apache2

您应该得到以下输出:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-06-17 12:09:42 UTC; 16s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 27602 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 27617 (apache2)
      Tasks: 6 (limit: 2353)
     Memory: 26.6M
     CGroup: /system.slice/apache2.service
             ??27617 /usr/sbin/apache2 -k start
             ??27618 /usr/sbin/apache2 -k start
             ??27619 /usr/sbin/apache2 -k start
             ??27620 /usr/sbin/apache2 -k start
             ??27621 /usr/sbin/apache2 -k start
             ??27622 /usr/sbin/apache2 -k start

Jun 17 12:09:42 ubuntu2004 systemd[1]: Starting The Apache HTTP Server...

访问 Polr Web 界面

您现在可以使用 URL http://polr.example.com 访问 Polr Web 界面。

如何在 Ubuntu 20.04 上安装 Polr 短链接服务的完整教程

如何在 Ubuntu 20.04 上安装 Polr 短链接服务的完整教程

如何在 Ubuntu 20.04 上安装 Polr 短链接服务的完整教程

如何在 Ubuntu 20.04 上安装 Polr 短链接服务的完整教程

提供您的数据库名称、用户名、密码、应用程序设置、Polr URL、管理员用户名、电子邮件、密码,然后单击 Install 按钮。Polr 安装完成后,您应该看到以下屏幕:

如何在 Ubuntu 20.04 上安装 Polr 短链接服务的完整教程

单击主页。您应该在以下屏幕上看到 Polr 仪表板:

如何在 Ubuntu 20.04 上安装 Polr 短链接服务的完整教程

使用 Let's Encrypt 免费 SSL 保护 Polr

首先,您需要安装 Certbot Let's Encrypt 客户端,以便为您的域名下载和安装 SSL 证书。

您可以使用以下命令安装它:

apt-get install python3-certbot-apache -y

安装完成后,您可以运行以下命令为您的域名 polr.example.com 安装 Let's Encrypt 证书。

certbot --apache -d polr.example.com

在安装过程中,系统会要求您提供电子邮件地址并接受服务条款,如下所示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for polr.example.com
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/polr-le-ssl.conf
Deploying Certificate to VirtualHost /etc/apache2/sites-available/polr-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/polr-le-ssl.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

接下来,输入 2 并按 Enter 键,为您的域名下载并安装免费的 SSL 证书。成功完成安装后,您应该得到以下输出:

Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/polr.conf to ssl vhost in /etc/apache2/sites-available/
polr-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://polr.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=polr.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

您的 Polr 网站现在已使用 Let's Encrypt 免费 SSL 保护。您可以使用 URL https://polr.example.com 访问它。

结论

在上面的指南中,您学习了如何在 Ubuntu 20.04 服务器上使用 Apache 和 Let's Encrypt SSL 安装 Polr。您现在可以使用 Polr 托管自己的链接缩短器。如果您有任何问题,请随时问我。