How to install YOURLS with caddy

YOURLS is an open-source program which can be used to shorten your url. It is a great url-shorten tool. However, the official instruction is only based on apache. For some specific reason, I must use caddy as my web server. Caddy and Apache cannot be used simultaneously as they occupy the same port. So I just try and succeed install YOURLS with caddy.

Environment

Debian12

caddy version: v2.7.5

How to install

1
2
3
4
5
6
7
8
9
10
11
wget https://github.com/YOURLS/YOURLS/archive/refs/tags/1.9.2.zip # Download the latest release archive from https://github.com/YOURLS/YOURLS/releases
mv 1.9.2.zip /var/www/ # move it to web folder
cd /var/www/
unzip 1.9.2.zip
rm 1.9.2.zip
mv YOURLS-1.9.2/ yourls #rename the folder
cd yourls/user
cp config-sample.php config.php # you need to set config in config.php then
apt install mariadb-server # for ubuntu, apt install mysql-server
apt install php php-mysql libapache2-mod-php php-cli php-fpm
mysql
1
2
3
4
CREATE DATABASE yourls;
CREATE USER 'yourls_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON yourls.* TO 'yourls_user'@'localhost';
EXIT;
1
vim config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

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

/** MySQL database password */
define( 'YOURLS_DB_PASS', 'your_password' );

/** The name of the database for YOURLS
** Use lower case letters [a-z], digits [0-9] and underscores [_] only */
define( 'YOURLS_DB_NAME', 'yourls' );

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

/** MySQL tables prefix
** YOURLS will create tables using this prefix (eg `yourls_url`, `yourls_options`, ...)
** Use lower case letters [a-z], digits [0-9] and underscores [_] only */
define( 'YOURLS_DB_PREFIX', 'yourls_' );

/*
** Site options
*/
define( 'YOURLS_SITE', 'http://your.domain' );

/** YOURLS language
** Change this setting to use a translation file for your language, instead of the default English.
** That translation file (a .mo file) must be installed in the user/language directory.
** See http://yourls.org/translations for more information */
define( 'YOURLS_LANG', '' );

/** Allow multiple short URLs for a same long URL
** Set to true to have only one pair of shortURL/longURL (default YOURLS behavior)
** Set to false to allow multiple short URLs pointing to the same long URL (bit.ly behavior) */
define( 'YOURLS_UNIQUE_URLS', true );

/** Private means the Admin area will be protected with login/pass as defined below.
** Set to false for public usage (eg on a restricted intranet or for test setups)
** Read http://yourls.org/privatepublic for more details if you're unsure */
define( 'YOURLS_PRIVATE', true );

/** A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated
** Hint: copy from http://yourls.org/cookie */
define( 'YOURLS_COOKIEKEY', 'copy the string from http://yourls.org/cookie or put some random string here' );

/** 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 */
$yourls_user_passwords = [
'username' => 'password',
// 'username2' => 'password2',
// You can have one or more 'login'=>'password' lines
];

/** URL shortening method: either 36 or 62
** 36: generates all lowercase keywords (ie: 13jkm)
** 62: generates mixed case keywords (ie: 13jKm or 13JKm)
** For more information, see https://yourls.org/urlconvert */
define( 'YOURLS_URL_CONVERT', 36 );

/** Debug mode to output some internal information
** Default is false for live site. Enable when coding or before submitting a new issue */
define( 'YOURLS_DEBUG', false );

/**
* Reserved keywords (so that generated URLs won't match them)
* Define here negative, unwanted or potentially misleading keywords.
*/
$yourls_reserved_URL = [
'porn', 'faggot', 'sex', 'nigger', 'fuck', 'cunt', 'dick',
];

/*
** Personal settings would go after here.
*/

1
2
3
chmod 0666 config.php # change the access permissions temporarily for auto-encrypt passwords, after first installation, you can change it back using chmod 0644 config.php, which is for safety concern.
cd /var/www/yourls/
vim .htaccess # In my installation method, this file will not generate automatically, so I need to create it manually.
1
2
3
4
5
6
7
8
9
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
1
2
cd ~
vim Caddyfile # Maybe your Caddyfile path is different from mine
1
2
3
4
5
6
7
your.domain {
root * /var/www/yourls
php_fastcgi unix//run/php/php8.2-fpm.sock # this can vary depends on your environment, you need to check the realpath of your php-fpm.sock
try_files {path} {path}/index.php /yourls-loader.php
# add the following config for handling the static file
file_server
}
1
caddy reload --config Caddyfile

Then just go to your.domain/admin with your browser and do installation following the instruction.

Reference

  1. [Caddy2 配置php]

  2. 【教程】如何搭建一个短网址平台 — YOURLS

  3. https://yourls.org/docs/guide/essentials/credentials#:~:text=an%20error%20message%3A%20%22-,Could%20not%20auto%2Dencrypt%20passwords,-%22%E2%80%8B

  4. https://yourls.org/docs/guide/server-configuration

  5. https://yourls.org/docs/guide/install

  6. https://github.com/YOURLS/YOURLS/issues/2141