Custom Local Development Domains with Apache on Debian-based Systems
Tags: linux, hacking, webdev
Published: 2023-08-29 08:48:50

I recently took a leap into the vast expanse of Linux, specifically choosing Parrot OS for my coding endeavors in security and web app development. The journey, to be honest, hasn’t exactly been a breeze. But, oddly enough, I've found the challenges invigorating. Every hiccup and every roadblock has been an opportunity to dive deeper, understand the intricacies of the OS, and ultimately grow as a coder. This constant learning and tinkering is, after all, the essence of what makes a simple coder like me a better hacker!

In this continuous voyage of discovery, one area that stood out as crucial, especially for those involved in web app development, is the setting up of custom local development domains. These domains not only streamline the development process but also mirror real-world scenarios, helping coders to better emulate and understand the deployment environment. In this guide, I'll walk you through how to set up these custom domains using Apache on Debian-based systems, drawing from my recent adventures with Parrot OS. 

## **What is a Local Domain?**

Picture the vast Star Wars galaxy. Each planet, like Tatooine or Hoth, has a unique name so that pilots and navigators know where to go. Now, imagine your computer as a mini-galaxy. In this mini-galaxy, instead of planets, you have websites.

A local domain is like naming one of those websites with a Star Wars planet name. Instead of visiting "127.0.0.1/tatooine," you'd visit "tatooine.local" or "tatooine.test"  on your browser, and it would take you to a website you're working on, right on your computer.

It's just a way to make navigation easier and more fun in your mini-galaxy of projects!

### Why did I use it for? 
Well, in Laravel development you are advised to run `php artisan serve` when you want to start developing your project, then you navigate to `127.0.0.1:8000` and there it is your precious little project. 
First of all, I hate running an extra command just to run a website... That's it really... I despise this extra step. Local domains solve that problem of mine so... here we are. 

## 彳1. Hosts File

The `hosts` file in Linux systems is a text file that maps hostnames to IP addresses, allowing computers to manually override DNS resolution for specific addresses. It's often used for local testing, development, or blocking certain websites. Located at `/etc/hosts`, users can add custom associations between hostnames and IP addresses, but caution is advised to prevent connectivity issues. 
Open the hosts file with `sudo nano /etc/hosts` and at the end of the `hosts` file add the desired name of your project: 


```text
127.0.0.1    sudorealm.test
```

this configuration maps the domain `sudorealm.test` to your local machine (127.0.0.1). 

## 彳2. Set up Apache configuration 

### Navigate to Apache's `sites-available` directory

```bash
cd /etc/apache2/sites-available/
```

### Create a new configuration file for the project

```bash 
sudo nano sudorealm.conf
```

```text
<VirtualHost *:80>
    ServerName sudorealm.test
    DocumentRoot /path/to/your/whatever/project/public

    <Directory /path/to/your/whatever/project/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
```

My project files are under the Code directory and I navigate to it with `~/Code/sudorealm/`  but please keep in mind that the `~` is a shorthand for a user's home directory. In configuration files, especially server configurations like Apache's, it's best to use the absolute path. This is because the server might not understand or resolve the `~` shorthand in the same way a shell would. Apache doesn't interpret `~` as the home directory, so using it might lead to configuration errors.

And just to be clear, I tried it and it didn't work. 😄

💾 **Save and close the file**
Press `CTRL + X` to save then press `Y` to exit (in nano).

### Enable the new configuration 

```bash
sudo a2ensite sudorealm.conf
```

### Ensure the `mod_rewrite` module is enabled

```bash
sudo a2enmod rewrite
```

### Restart Apache

```bash
sudo systemctl restart apache2
```

Now if you've updated your `/etc/hosts/` file appropriately and as mentioned, you should be able to access `http://sudorealm.test` in your browser. 

## Possible step: ⼻ No Permissions, Forbidden! 

There are some times where you come across this pesky message while working on your local setup:

```text
Forbidden
You don't have permission to access this resource.

Apache/2.4.56 (Debian) Server at sudorealm.test Port 80
```

No need for panic! 🚀 This usually indicates a permission issue. Here's a quick solution:

- **Assign Proper Ownership**: Make sure your project and its sub-files are in the good hands of the web server user, which is commonly `www-data` on Debian-flavored systems.
```bash
sudo chown -R www-data:www-data /path/to/your/project/public
```

- **Set Read and Execute Permissions**: It's equally important for the Apache user to have the right to read your Laravel project files and stroll through its directories.

```bash
sudo chown -R yourusername:www-data /home/yourusername/Code/projectFolder
sudo chmod -R 775 /home/yourusername/Code/projectFolder
```

- **Give Apache a Gentle Nudge**: Lastly, remember to refresh Apache's memory to ensure it's up-to-date with these changes.

## Possible step: ⼻ Wrong php version on Composer!

Since I am using Laravel I got the following message: 
```text
Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.1.0"
``` 

If you face this, even after confirming the correct PHP version using the `php -v` command, it means that Apache is still referencing the outdated PHP version. But not to worry, this can be easily rectified!
Here's a step-by-step to align Apache with the desired PHP version:
```bash
sudo a2dismod php7.4
sudo a2enmod php8.1
sudo service apache2 restart
```

With these commands, Apache is set to use PHP 8.1, ensuring smoother development experiences with php8.1.

## 😎 Hacking story. 

**The Stage:**
Marla, our InstaGlam diva, was living her best online life. Filters, fans, and fabulousness – she had it all.

**The Sneaky Deed:**
Find out in the [Sudorealm Post](https://sudorealm.com/blog/setting-up-custom-local-development-domains-with-apache-on-debian-based-systems-a-comprehensive-guide)😎

## 🚀 Spread the Love & Support the Realm

Hey there, fellow Realmer! If this guide illuminated a new path in your Linux/hacker journey, your support would mean a lot. Every bit of magic helps

👑 **Crown & Share**: If you found value in this post, please give it a crown and share it with your fellow code enthusiasts. Spreading knowledge is what Sudorealm is all about! *Fun fact the Author with the most crowns inside a realm will be crowned as the Realm King!* 🤴

🛍 **Affiliate Treasures Below**: Dive into the depths below the post in [sudorealm.com](https://sudorealm.com/blog/setting-up-custom-local-development-domains-with-apache-on-debian-based-systems-a-comprehensive-guide) to uncover some affiliate products I've curated just for you. It's a great way to support the realm and discover some nerdy treasures.

🐦 **Twitter/X Shoutout**: Feeling extra grateful or have some cool feedback? Drop me a shoutout on Twitter – I'd love to hear from you!

☕️ **Coffee Driven Development**: Enjoyed the content? Help keep my coding sessions energized with a cuppa! **[BuyMeACoffee](buymeacoffee.com/ZixcW8N)**

Thanks for being a part of our realm. Every bit of support propels our community to new horizons. Until next time, keep exploring!

I am [@d3ad R1nger](https://sudorealm.com/blog/profile/d3ad-r1nger), Creator of Sudorealm, thank you for reading.