Create your own URL shortener!
Tags: ubuntu, tutorial, website, webdev
Published: 2023-08-17 08:48:52

Today we come to a very interesting topic in the field of HomeServer.

---

If you like our articles, we would appreciate a Like & Comment:heart:

---
We want to install our own URL shortener on our HomeSever. This means that shortened links can then be reached under your domain/desired name. There are several options here, e.g. you can also generate the shortened links randomly or provide them with numbers. But more on that. You can do it!

Or do you like to test it first?
Then try it below
[url.schächner.de](url.schächner.de)!

## Database
First we create the databases and users again. To do that, let's switch to first

`mysql`
and then create the database with

`CREATE DATABASE yourls;`
and then the user with

`CREATE USER 'yourlsuser'@'localhost' IDENTIFIED BY 'password';`
and the corresponding password.

We'll give this to you

`GRANT ALL PRIVILEGES ON yourls.* TO 'yourlsuser'@'localhost';`
the permissions and load them as well

`FLUSH PRIVILEGES;`
new.

Now we can do that

`EXIT;`
leave again.

## Download Yourls
Now let's switch

`cd /tmp`
Into the temporary directory we also download YOURLS with

`sudo wget https://github.com/YOURLS/YOURLS/archive/refs/tags/1.9.1.zip`

We'll unpack that
`unzip 1.9.1.zip`
and move it to the /var/www/ directory

`sudo mv YOURLS-1.9.1 /var/www/`
If we want, we can also add the folder

`cd /var/www/`
sudo mv YOURLS-1.9.1 yourls
rename.

## config file
Now we have to go to the directory

`cd yourls/user`
switch and copy the config file there with

`cp config-sample.php config.php`
We now have to edit this with

`sudo nano config.php`
Here we need to adjust the following values:

`define( 'YOURLS_DB_USER', 'yourlsuser' );`
In the line above we need to enter our database user.

In the following line the database password

`define( 'YOURLS_DB_PASS', 'password');`
and the name of the database

`define( 'YOURLS_DB_NAME', 'yourls' );`
Here

`define( 'YOURLS_SITE', 'yourls.example.com' );`
we have to enter our desired domain.

With `ctrl + w` we can also search for the following value:

`$yourls_user_passwords = [`
A user and a password are already specified below, which must be changed to the desired values.

## Permissions
Now we need to set the permissions again with

`sudo chown -R www-data:www-data /var/www/yourls`
and

`sudo chmod -R 775 /var/www/yourls`

## Apache2 config file
We have to take these with us

`sudo nano /etc/apache2/apache2.conf`
edit.

There we search again with `ctrl + w`

`AllowOverride`
We need all of these values, except for the area with `<Directory /var/www/>` from

```
AllowOverride None
```
to

```
AllowOverrideAll
```
adjust.

We insert the following block above the area with `<Directory /var/www/>`:

```js
<Directory /var/www/yourls/>
         Options Indexes FollowSymLinks
         AllowOverrideAll
         Require all granted
</Directory>
virtual host
Now we create a virtual host with

sudo nano /etc/apache2/sites-available/yourls.conf
and add the following content:

<VirtualHost *:80>
     ServerName yourls.example.com
     DocumentRoot "/var/www/yourls"

     <Directory "/var/www/yourls">
         Require all granted
         Options Indexes FollowSymLinks
         AllowOverrideAll
         Order allow deny
         Allow from all
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
   
</VirtualHost>
```
Of course, as always, we have to take this file with us

`sudo a2ensite yourls.conf`
activate.

Now we also start the web server

`sudo service apache2 restart`
and

`sudo systemctl restart apache2.service`
new.

Now we can go into the browser to configuration!

This can be done by opening the domain (only if we have defined an A record or AAAA record) /admin or ip/admin in the browser.

Here we go!

PS:

Do you want a certificate for this instance?

This is what you get with our article:

[Make NextCloud accessible from the Internet and an SSL certificate or https for our cloud and other web installations - Schächner](https://schächner.de/2022/08/03/nextcloud-aus-dem-internet-erreichbar-machen-und-ein-ssl-zertifikat-bzw-https-fuer-unsere-cloud-und-andere-web-installationen/)

Thanks for reading,
Schächner