YOURLS代表您自己的URL缩短器,这是一个免费的开源PHP脚本,可让您创建自定义URL缩短服务。它允许您创建简短和自定义的URL,跟踪点击统计信息并控制您的数据。
在 Ubuntu 22.04 LTS Jammy Jellyfish 上安装 YOURLS
第 1 步。在开始安装过程之前,最好更新系统以确保我们拥有最新的软件包。为此,请打开终端并运行以下命令:
<span class="pln">sudo apt update sudo apt upgrade sudo apt install wget apt</span><span class="pun">-</span><span class="pln">transport</span><span class="pun">-</span><span class="pln">https gnupg2</span>
第 2 步。在 Ubuntu 22.04 上安装 LAMP 堆栈。
YOURLS需要Web服务器,数据库和PHP才能运行。如果您没有安装 LAMP 堆栈,您可以按照我们的指南进行操作。
第 3 步。配置 MariaDB。
默认情况下,MariaDB 未强化。您可以使用脚本保护 MariaDB。您应该仔细阅读并在每个步骤下方仔细阅读,这将设置root密码,删除匿名用户,禁止远程root登录,并删除测试数据库和对安全MariaDB的访问权限:mysql_secure_installation
<span class="pln">mysql_secure_installation</span>
像这样配置它:
<span class="pun">-</span> <span class="typ">Set</span><span class="pln"> root password</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Remove</span><span class="pln"> anonymous users</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Disallow</span><span class="pln"> root login remotely</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Remove</span><span class="pln"> test database </span><span class="kwd">and</span><span class="pln"> access to it</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y </span><span class="pun">-</span> <span class="typ">Reload</span><span class="pln"> privilege tables now</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y</span>
接下来,我们需要登录到 MariaDB 控制台并为 YOURLS 创建一个数据库。运行以下命令:
<span class="pln">mysql </span><span class="pun">-</span><span class="pln">u root </span><span class="pun">-</span><span class="pln">p</span>
- 这将提示您输入密码,因此请输入您的MariaDB root密码并按Enter键。登录到数据库服务器后,您需要为Yourls安装创建一个数据库:
<span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> create database yourlsdb character </span><span class="kwd">set</span><span class="pln"> utf8mb4</span><span class="pun">;</span> <span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> grant all on yourlsdb</span><span class="pun">.*</span><span class="pln"> to </span><span class="str">'yourls'</span><span class="pun">@</span><span class="str">'localhost'</span><span class="pln"> identified </span><span class="kwd">by</span> <span class="str">'your-strong-passwd'</span><span class="pun">;</span> <span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> flush privileges</span><span class="pun">;</span> <span class="typ">MariaDB</span> <span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span> <span class="kwd">exit</span><span class="pun">;</span>
有关安装 MariaDB 的其他资源,请阅读以下帖子:
第 4 步。在 Ubuntu 22.04 上安装 YOURLS。
默认情况下,YOURLS 在 Ubuntu 22.04 基础存储库上不可用。现在运行以下命令,从官网下载最新版本的YOURLS:
<span class="pln">wget https</span><span class="pun">:</span><span class="com">//github.com/YOURLS/YOURLS/archive/refs/tags/1.9.2.tar.gz 接下来,提取下载的存档: </span>
<span class="pln">sudo tar </span><span class="pun">-</span><span class="pln">xvzf </span><span class="lit">1.9</span><span class="pun">.</span><span class="lit">2.tar</span><span class="pun">.</span><span class="pln">gz sudo mv YOURLS</span><span class="pun">-</span><span class="lit">1.9</span><span class="pun">.</span><span class="lit">2</span><span class="com">/* /var/www/yourls/ sudo rm -rf YOURLS-1.9.2/</span>
之后,将 YOURLS 目录的所有权更改为 Apache 用户:
<span class="pln">sudo chown </span><span class="pun">-</span><span class="pln">R www</span><span class="pun">-</span><span class="pln">data</span><span class="pun">:</span><span class="pln">www</span><span class="pun">-</span><span class="pln">data </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">yourls sudo chmod </span><span class="pun">-</span><span class="pln">R </span><span class="lit">755</span> <span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">yourls sudo chmod </span><span class="lit">777</span> <span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">yourls</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">php</span>
接下来,复制到:user/config-sample.php
user/config.php
<span class="pln">cp user</span><span class="pun">/</span><span class="pln">config</span><span class="pun">-</span><span class="pln">sample</span><span class="pun">.</span><span class="pln">php user</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">php nano user</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">php</span>
编辑以下行以匹配数据库配置:
<span class="pun">*</span> <span class="pun">**</span> <span class="typ">MySQL</span><span class="pln"> settings </span><span class="pun">-</span> <span class="typ">You</span><span class="pln"> can </span><span class="kwd">get</span> <span class="kwd">this</span><span class="pln"> info </span><span class="kwd">from</span><span class="pln"> your web host </span><span class="pun">*/</span> <span class="com">/** MySQL database username */</span><span class="pln"> define</span><span class="pun">(</span> <span class="str">'YOURLS_DB_USER'</span><span class="pun">,</span> <span class="str">'</span><strong><span class="str">yourls</span></strong><span class="str">'</span> <span class="pun">);</span> <span class="com">/** MySQL database password */</span><span class="pln"> define</span><span class="pun">(</span> <span class="str">'YOURLS_DB_PASS'</span><span class="pun">,</span> <span class="str">'</span><strong><span class="str">your-strong-passwd</span></strong><span class="str">'</span> <span class="pun">);</span> <span class="com">/** The name of the database for YOURLS */</span><span class="pln"> define</span><span class="pun">(</span> <span class="str">'YOURLS_DB_NAME'</span><span class="pun">,</span> <span class="str">'</span><strong><span class="str">yourlsdb</span></strong><span class="str">'</span> <span class="pun">);</span> <span class="com">/** MySQL hostname. ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */</span><span class="pln"> define</span><span class="pun">(</span> <span class="str">'YOURLS_DB_HOST'</span><span class="pun">,</span> <span class="str">'</span><strong><span class="str">localhost</span></strong><span class="str">'</span> <span class="pun">);</span> <span class="com">/** MySQL tables prefix */</span><span class="pln"> define</span><span class="pun">(</span> <span class="str">'YOURLS_DB_PREFIX'</span><span class="pun">,</span> <span class="str">'</span><strong><span class="str">yourls_</span></strong><span class="str">'</span> <span class="pun">);</span>
为 YOURLS 设置网站网址:
<span class="com">/** YOURLS installation URL -- all lowercase, no trailing slash at the end. ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */</span><span class="pln"> define</span><span class="pun">(</span> <span class="str">'YOURLS_SITE'</span><span class="pun">,</span> <span class="str">'</span><strong><span class="str">http://yourls.your-domain.com</span></strong><span class="str">'</span> <span class="pun">);</span>
接下来,设置用户和密码:
<span class="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 http://yourls.org/userpassword for more information */</span><span class="pln"> $yourls_user_passwords </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">(</span> <span class="str">'admin'</span> <span class="pun">=></span> <span class="str">'</span><strong><span class="str">meilana</span></strong><span class="str">'</span><span class="pun">,</span> <span class="str">'jmutai'</span> <span class="pun">=></span> <span class="str">'</span><strong><span class="str">meilana-Strong-Password</span></strong><span class="str">'</span><span class="pun">,</span> <span class="com">// You can have one or more 'login'=>'password' lines</span> <span class="pun">);</span>
保存并关闭文件。
第5步。配置 Apache 虚拟主机。
现在为 YOURLS 创建虚拟主机配置文件:
<span class="pln">nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">sites</span><span class="pun">-</span><span class="pln">available</span><span class="pun">/</span><span class="pln">yourls</span><span class="pun">.</span><span class="pln">conf</span>
添加以下文件:
<span class="tag"><VirtualHost</span><span class="pln"> *:80</span><span class="tag">></span><span class="pln"> ServerName url.unixcop.com DocumentRoot /var/www/yourls </span><span class="tag"><Directory</span> <span class="pun">/</span><span class="atn">var</span><span class="pun">/</span><span class="atn">www</span><span class="pun">/</span><span class="atn">yourls</span><span class="tag">/></span><span class="pln"> Options FollowSymlinks AllowOverride All Require all granted </span><span class="tag"></Directory></span><span class="pln"> ErrorLog ${APACHE_LOG_DIR}/yourls.your-domain.com_error.log CustomLog ${APACHE_LOG_DIR}/yourls.your-domain.com_access.log combined </span><span class="tag"></VirtualHost></span>
保存并关闭文件,然后重新启动 Apache,以便进行更改:
<span class="pln">sudo a2enmod rewrite sudo a2ensite yourls</span><span class="pun">.</span><span class="pln">conf sudo systemctl restart apache2</span>
有关安装和管理 Apache 的其他资源,请阅读下面的帖子:
- 如何在 Ubuntu Linux √ 上安装 Apache。
第 6 步。使用Let’s Encrypt SSL保护YOURLS。
首先,使用以下命令安装 Certbot 客户端:
<span class="pln">sudo apt install certbot python3</span><span class="pun">-</span><span class="pln">certbot</span><span class="pun">-</span><span class="pln">apache2</span>
接下来,按照以下步骤使用Let’s Encrypt获取SSL证书:
<span class="pln">certbot </span><span class="pun">--</span><span class="pln">apache </span><span class="pun">-</span><span class="pln">d yourls</span><span class="pun">.</span><span class="pln">your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com</span>
让我们加密证书的有效期为 90 天,强烈建议在证书过期之前续订证书。您可以通过运行以下命令来测试证书的自动续订:
<span class="pln">sudo certbot renew </span><span class="pun">--</span><span class="pln">dry</span><span class="pun">-</span><span class="pln">run</span>
步骤 7.配置防火墙。
现在我们使用Apache设置了一个简单防火墙(UFW),以允许在HTTP和HTTPS的默认Web端口上进行公共访问:
<span class="pln">sudo ufw allow </span><span class="typ">OpenSSH</span><span class="pln"> sudo ufw allow </span><span class="str">'Apache Full'</span><span class="pln"> sudo ufw enable</span>
第8步。访问 YOURLS 网页界面。
成功安装后,现在打开您的网络浏览器并使用 URL 访问 YOURLS Web UI。您将被重定向到以下页面:https://yourls.your-domain.com
感谢您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系统上安装 YOURLS(您自己的 URL 缩短器)。如需其他帮助或有用信息,我们建议您查看 YOURLS 网站。