If you’re tired of relying on third-party URL shorteners or just always wanted to host your own link shortener like me, YOURLS is a solid choice. It’s a self-hosted PHP application that gives you your own URL shortening service, complete with analytics and custom short links.
The setup is straightforward. You’ll need a VM instance, a domain name pointing to it, and an SSL certificate. Most cloud providers offer free tier VMs that work perfectly for this. I went with Azure for this guide, but the steps are nearly identical for AWS or Oracle Cloud. Compare free tier options on the Cloud Free Tier Comparison List to see what fits your needs.
Prerequisites
- A VM instance (Azure, AWS, Oracle Cloud, etc.)- A domain name configured to point to your server
- SSL certificate (preferably)
Step 1: Setting Up Your Azure Instance
Create a Virtual Machine
- Log into the Azure Portal and navigate to “Virtual Machines”
- Click “Create” → “Azure virtual machine”

- Fill in the basic details:
- Subscription: Select your subscription
- Resource Group: Create new or use existing
- Virtual machine name: Choose a name (e.g.,
yourls-server) - Region: Choose a region close to you
- Image: Select “Ubuntu Server 22.04 LTS”
- Size: For the free tier, select “Standard_B1s” (1 vCPU, 1 GiB RAM)

Under “Administrator account”:
- Authentication type: SSH public key (recommended) or Password
- Username: Choose a username (e.g.,
azureuser) - SSH public key source: Generate new key pair or use existing
Under “Inbound port rules”:
- Public inbound ports: Allow selected ports
- Select inbound ports: SSH (22), HTTP (80), HTTPS (443)

- Review and create the VM. This may take a few minutes.
Connect to Your Server
Once your VM is created, you can connect via SSH:
| |
Replace YOUR_VM_PUBLIC_IP with the public IP address shown in the Azure portal.
Update System Packages
Once connected, update your system:
| |
Now that your server is ready, let’s get the web stack installed. YOURLS needs Apache, MySQL, and PHP to run properly.
Step 2: Install Required Software
Install Apache, MySQL/MariaDB, and PHP:
Install Apache
| |
Install MySQL
| |
Secure your MySQL installation:
| |
Follow the prompts to set a root password and configure security settings.
Install PHP and Required Extensions
| |
Verify PHP installation:
| |
You should see PHP 8.1 or later.
With all the software installed, we need to enable a few Apache modules that YOURLS depends on for URL rewriting and SSL.
Step 3: Configure Apache Modules
Enable the required Apache modules for YOURLS:
| |
Verify Services
Check that all services are running:
| |
Everything should be running smoothly now. Time to grab the YOURLS files and get them set up on your server.
Step 4: Download YOURLS
Download the latest YOURLS release:
| |
Step 5: Set Up Web Directory
Create a directory for your domain and copy YOURLS files:
| |
Before we configure YOURLS, we need to set up the database. YOURLS stores all your links and stats in MySQL, so let’s create a dedicated database and user for it.
Step 6: Create MySQL Database and User
Generate Secure Password
| |
Important: Save this password securely - you’ll need it for the configuration file.
Create Database and User
| |
Step 7: Generate Security Keys
Generate a cookie encryption key and an admin password:
Generate Cookie Key
| |
Generate Admin Password
| |
Important: Save both the cookie key and admin password securely. You’ll need them to log into the admin panel.
Now create the configuration file that tells YOURLS how to connect to the database.
Step 8: Create Configuration File
| |
Or create it directly with a here-document:
| |
Replace the placeholders with your actual values:
| |
Note: Replace yourdomain.com with your actual domain name in the last command and in the config file.
Step 9: Set File Permissions
| |
Step 10: Create .htaccess File
| |
Next up, we need to configure Apache to serve your domain. This involves setting up virtual hosts for both HTTP and HTTPS.
Step 11: Configure Apache Virtual Host
Create HTTP Virtual Host (Port 80)
| |
Add the following content:
| |
Create HTTPS Virtual Host (Port 443)
| |
Add the following content:
| |
Enable Sites
| |
Step 12: SSL Certificate Setup
You have two main options for SSL certificates:
Option A: Using Let’s Encrypt (Recommended - Free)
Let’s Encrypt provides free SSL certificates with auto-renewal:
| |
Certbot will automatically configure Apache with SSL and set up auto-renewal.
Option B: Using a Commercial SSL Certificate
If using a commercial SSL certificate from your domain registrar:
- Upload your certificate files to the server
- Update the SSL paths in
/etc/apache2/sites-available/yourdomain.com-ssl.conf:SSLCertificateFile: Path to your certificate fileSSLCertificateKeyFile: Path to your private key fileSSLCertificateChainFile: Path to your chain file (if provided)
Once SSL is configured, test Apache before pointing DNS to the server.
Step 13: Test and Start Apache
| |
If the test passes:
| |
Apache is configured and running. Point your domain to this server to access YOURLS.
Step 14: Configure DNS
Log into your domain registrar’s DNS management panel
Create an A record pointing your domain to your Azure VM’s public IP address:
- Type: A
- Name: @ (or yourdomain.com)
- Value: YOUR_VM_PUBLIC_IP
- TTL: 3600 (or default)
Optionally, create a CNAME record for www:
- Type: CNAME
- Name: www
- Value: yourdomain.com
- TTL: 3600
DNS propagation can take anywhere from a few minutes to 48 hours, though it’s usually much faster.
Step 15: Verify Installation
Once DNS has propagated:
Test Database Connection
| |
If this runs without errors, your database connection is working.
Access Admin Panel
- Open your browser and navigate to:
https://yourdomain.com/admin/ - You should see the YOURLS setup page

- Run the automatic installation

Reload the page
Login with:
- Username:
admin(or whatever you set in config.php) - Password: The admin password you generated in Step 7
- Username:

Test Creating a Short URL
Once logged in, create a short URL to verify everything works:
- Enter a long URL in the “Enter a URL” field
- Optionally, specify a custom keyword
- Click “Shorten The URL”

Troubleshooting
Apache Returns 403 Forbidden
- Check file permissions:
sudo chown -R www-data:www-data /var/www/yourdomain.com - Verify
AllowOverride Allis set in Apache config - Check Apache error logs:
sudo tail -f /var/log/apache2/error.log
Database Connection Errors
- Verify database exists:
sudo mysql -e "SHOW DATABASES;" - Check user permissions:
sudo mysql -e "SHOW GRANTS FOR 'yourls_user'@'localhost';" - Verify password in config.php matches the database user password
SSL Certificate Issues
- Ensure mod_ssl is enabled:
sudo a2enmod ssl - Check certificate paths are correct
- Verify certificate files are readable:
sudo ls -la /path/to/certificate - For Let’s Encrypt, check renewal:
sudo certbot renew --dry-run
Short URLs Not Working
- Verify mod_rewrite is enabled:
sudo a2enmod rewrite - Check .htaccess file exists and has correct permissions
- Ensure
AllowOverride Allis set in Apache Directory directive - Check Apache error logs for specific rewrite errors
Can’t Access Site After DNS Change
- Verify DNS propagation:
dig yourdomain.comor use online DNS checkers - Check that your firewall allows HTTP (80) and HTTPS (443) traffic
- Verify Apache is running:
sudo systemctl status apache2 - Check that your virtual hosts are enabled:
sudo apache2ctl -S
Security Best Practices
- Keep credentials secure: Store database passwords and admin credentials in a password manager
- Regular updates: Keep YOURLS, PHP, Apache, and MySQL updated:
1sudo apt update && sudo apt upgrade -y - Backup regularly: Set up automated backups for your database and config files
- File permissions: Never make config.php world-writable (644 is sufficient)
- Firewall: Configure Azure Network Security Groups to only allow necessary ports
- SSH security: Consider disabling password authentication and using SSH keys only
File Locations Summary
- YOURLS Installation:
/var/www/yourdomain.com/ - Configuration File:
/var/www/yourdomain.com/user/config.php - Apache Config (HTTP):
/etc/apache2/sites-available/yourdomain.com.conf - Apache Config (HTTPS):
/etc/apache2/sites-available/yourdomain.com-ssl.conf - .htaccess:
/var/www/yourdomain.com/.htaccess - Database:
yourls_db(MySQL) - Database User:
yourls_user@localhost
Next Steps
Now that everything is working, here’s what you can do next:
- Log into the admin panel at
https://yourdomain.com/admin/ - Test creating a short URL
- Customize your YOURLS installation with plugins from the YOURLS plugin directory
- Set up regular backups (consider automating with cron jobs)
- Monitor your installation for security updates
- Configure analytics if desired
- Set up email notifications for new short URLs (if using plugins)
Conclusion
That’s it. You’ve got your own URL shortener running. YOURLS is flexible once you start digging into plugins and customization. Since we’re using free tier VMs, this setup costs nothing to run, perfect for personal projects or small teams.
Keep everything updated and set up backups. The last thing you want is losing all your links because you forgot to back up the database.