如何在 Debian 10 上安装 Polr Link Shortener
在此页
- 先决条件
- 开始
- 安装 LAMP 服务器
- 配置 MariaDB 数据库
- 安装作曲家
- 安装 Polr
- 为 Polr 配置 Apache
- 访问 Polr Web 界面
- 使用 Lets Encrypt 免费 SSL 保护 Polr
- 结论
Polr 是一个免费的开源 URL 缩短器,允许您托管自己的链接缩短器。它是用 PHP 和 Lumen 编写的,并使用 MariaDB 来存储其数据。使用 Polr,您可以在不破坏现有代码的情况下创建新链接或查找现有链接。
在本教程中,我们将向您展示如何在 Debian 10 服务器上安装 Polr Link Shortener 软件。
先决条件
- 运行 Debian 10 的服务器。
- 用您的服务器 IP 指向的有效域名。在本教程中,我们将使用 polr.example.com 域。
- 在您的服务器上配置了根密码。
入门
在开始之前,最好使用最新版本更新您的系统。您可以使用以下命令更新您的系统:
apt-get update -y
apt-get upgrade -y
更新系统后,重新启动它以实施更改。
安装 LAMP 服务器
首先,您需要在系统中安装 Apache 网络服务器、MariaDB 服务器、PHP 和其他软件包。您可以使用以下命令安装所有这些:
apt-get install apache2 mariadb-server libapache2-mod-php php php-pdo php-mysql php-mbstring php-tokenizer php-json php-curl git wget unzip -y
安装完所有包后,启动 Apache 和 MariaDB 服务并使它们能够在系统重启后启动。
systemctl start apache2
systemctl start mariadb
systemctl enable apache2
systemctl enable mariadb
完成后,您可以继续下一步。
配置 MariaDB 数据库
默认情况下,Debian 10 中没有配置 MariaDB root 密码。因此,您需要先设置它。
为此,请使用以下命令登录到 MariaDB shell:
mysql
登录后,使用以下命令设置您的 MariaDB root 密码:
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("newpassword");
接下来,使用以下命令为 Polr 创建数据库和用户:
MariaDB [(none)]> CREATE DATABASE polrdb;
MariaDB [(none)]> GRANT ALL ON polrdb.* TO 'polr' IDENTIFIED BY 'password';
接下来,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
完成后,您可以继续下一步。
安装作曲家
接下来,您需要在系统中安装 Composer。 Composer 是 PHP 的依赖管理器,用于为您的项目安装所有 PHP 依赖项。
首先,使用以下命令下载 Composer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
接下来,您需要通过将脚本 SHA-384 哈希与最新哈希进行比较来验证下载脚本的数据完整性。
为此,请从 Git 存储库下载最新 Composer 安装程序的签名并将其存储在名为 HASH 的变量中:
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
接下来,使用以下命令验证安装脚本:
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
如果哈希匹配,您将看到以下输出:
Installer verified
最后,使用以下命令安装 Composer:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
安装完成后,您应该会看到以下输出:
All settings correct for using Composer
Downloading...
Composer (version 1.9.2) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
完成后,您可以继续下一步。
安装 Polr
首先,从 Git 仓库下载最新版本的 Polr 到 Apache web 根目录:
cd /var/www/html
git clone https://github.com/cydrobolt/polr.git --depth=1
下载后,将目录更改为 polr 并使用以下命令安装所有必需的依赖项:
cd polr
composer install --no-dev -o
接下来,通过重命名以下文件启用基于 Web 的安装程序:
mv .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
DocumentRoot /var/www/html/polr/public
ServerName polr.example.com
<Directory /var/www/html/polr/public/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/polr_error.log
CustomLog ${APACHE_LOG_DIR}/polr_access.log combined
</VirtualHost>
完成后保存并关闭文件。然后,启用 Apache 虚拟主机文件并使用以下命令重写模块:
a2ensite polr.conf
a2enmod rewrite
最后,重启 Apache 服务以实现更改:
systemctl restart apache2
至此,Polr 已安装并配置完毕。您现在可以继续下一步。
访问 Polr Web 界面
现在,打开您的 Web 浏览器并输入 URL http://polr.example.com。您将被重定向到 Polr 设置向导,如下所示:




提供您的数据库信息、应用程序设置、管理员帐户设置,然后单击“安装”按钮。安装完成后,您应该会看到以下页面:

单击登录按钮。您将被重定向到 Polr 登录页面:

提供您的管理员用户名和密码,然后单击“登录”按钮。您应该会看到以下页面:


现在,单击右上角的 ADMIN => DASHBOARD。您应该在以下页面中看到 Polr 仪表板:

使用 Lets Encrypt 免费 SSL 保护 Polr
首先,您需要安装 Certbot Lets Encrypt 客户端来为您的域下载和安装 SSL 证书。
您可以使用以下命令安装它:
apt-get install python-certbot-apache -y
安装后,您可以运行以下命令为您的域 polr.example.com 安装 Lets 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):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 网站现在受到 Lets Encrypt 免费 SSL 的保护。您可以使用 URL https://polr.example.com 访问它。
结论
恭喜!您已在 Debian 10 服务器上成功安装 Polr 并使用 Lets Encrypt SSL 对其进行保护。您现在可以使用 Polr Web 界面托管您自己的链接缩短器。如果您有任何问题,请随时问我。