In this guide, we'll configure Nginx to handle wildcard subdomains (e.g., *.test), install the Laravel CLI for project creation, and create a Bash script to automate setting up new projects with custom subdomains. By the end, you'll have a local development environment where you can access sites like project1.test, project2.test, etc., with ease. Let’s get started!
- A Linux system (e.g., Ubuntu)
- Basic command-line knowledge
- Root or sudo privileges
- Composer installed (
sudo apt install composerif not already present)
First, create a directory to store all your projects. We’ll call it Herd and place it in your home directory.
mkdir /home/$USER/HerdThis folder will hold each project’s files, organized by subdomain.
Next, install Nginx to manage your web server and wildcard subdomains.
sudo apt install nginxThis installs Nginx and its dependencies. It will start automatically after installation.
Let’s set up Nginx to handle wildcard subdomains by creating a configuration file.
-
Create a new file called
wildcard.confin the/etc/nginx/sites-available/directory:sudo nano /etc/nginx/sites-available/wildcard.conf
-
Paste the following configuration:
server { listen 80; # Extract the subdomain from the host set $subdomain $host; if ($subdomain ~* ^([^.]+)\.test$) { set $subdomain $1; } # Set the root directory based on the subdomain root /home/$USER/Herd/$subdomain/public; # Define default index files (add index.php for PHP support) index index.php index.html index.htm index.nginx-debian.html; # Match all subdomains of .test server_name *.test; # Serve files or fall back to index.php location / { try_files $uri $uri/ /index.php?$query_string; } # Handle PHP files location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Deny access to .ht files (e.g., .htaccess) location ~ /\.ht { deny all; } # Optional: Custom logging per subdomain access_log "/var/log/nginx/${subdomain}_access.log"; error_log "/var/log/nginx/${subdomain}_error.log"; }
-
Save and exit.
server_name *.test: Matches any subdomain of.test(e.g.,project1.test).$subdomain: Extracts the subdomain (e.g.,project1) from the request.root: Points to the project’spublicfolder in~/Herd/<subdomain>/public.- PHP support is included via FastCGI, assuming PHP 8.3 is installed (adjust the socket path if your version differs).
-
Link the config to
sites-enabled:sudo ln -s /etc/nginx/sites-available/wildcard.conf /etc/nginx/sites-enabled/
-
Test the Nginx configuration for errors:
sudo nginx -t
If you see "syntax is ok" and "test is successful," you’re ready to proceed.
-
Reload Nginx to apply the changes:
sudo systemctl reload nginx
To create Laravel projects easily, install the Laravel CLI globally using Composer. This tool simplifies project setup and is used in our automation script.
-
Run the following command:
composer global require laravel/installer
-
Ensure Composer’s global bin directory is in your PATH. Add this line to your
~/.bashrcor~/.zshrc(depending on your shell):export PATH="$HOME/.composer/vendor/bin:$PATH"
-
Apply the PATH update:
source ~/.bashrc
-
Verify the installation:
laravel --version
You should see the Laravel installer version (e.g.,
Laravel Installer 4.x.x).
Now, let’s create a script to automate project setup and subdomain configuration.
-
Create a file called
herd.sh:nano herd.sh
-
Paste this script:
#!/bin/bash # Check if a project name is provided if [ -z "$1" ]; then echo "Error: Project name is required!" echo "Usage: $0 <project-name>" exit 1 fi # Create a new Laravel project in ~/Herd cd ~/Herd && laravel new "$1" # Add subdomain to /etc/hosts line_to_append="127.0.0.1 $1.test" echo "$line_to_append" | sudo tee -a /etc/hosts > /dev/null # Update the APP_URL in the project’s .env file cd ~/Herd/"$1" && sed -i "s|^APP_URL=.*|APP_URL=http://${1}.test|" .env # Notify the user echo "You can access it using: http://$1.test"
-
Save and exit.
-
Make the script executable:
chmod +x herd.sh
To create a new project, run:
./herd.sh myproject- Creates a new Laravel project in
~/Herd/myprojectusing the Laravel CLI. - Adds
127.0.0.1 myproject.testto/etc/hostsfor local DNS. - Updates the
.envfile withAPP_URL=http://myproject.test. - Outputs the URL you can use to access it.
Now, open your browser and visit http://myproject.test—it should work seamlessly!
- PHP Dependency: The Nginx config assumes PHP 8.3 FPM is installed. Install it with
sudo apt install php8.3-fpmif needed (adjust version as necessary). - Composer: Ensure Composer is installed (
sudo apt install composer) before installing the Laravel CLI. - DNS: This setup uses
/etc/hostsfor local testing. For production, configure a wildcard DNS record (e.g.,*.test) with your DNS provider.
With Nginx handling wildcard subdomains, the Laravel CLI installed, and an automation script in place, you can create new projects in seconds. This setup is perfect for local development, testing multiple apps, or even prototyping multi-tenant systems. Happy coding!
