Setting up a Laravel project can be repetitive and time-consuming. To address this, I’ve developed a custom installation script based on my experience, using my Project Template.
This script automates the process of configuring your environment, creating the database, managing dependencies, and setting up Supervisor to manage Laravel queues—saving you time and ensuring consistency.
Introduction
When setting up a new Laravel project, the following steps are often required:
- Configuring environment variables.
- Installing dependencies via Composer and npm.
- Creating the database.
- Setting up Supervisor to manage processes like Horizon.
To streamline this process, the installation script in the bin/
directory automates these tasks, ensuring everything is set up properly in one go.
Where to Find the Installation Script
The main installation script is located at:
-
Installation Script:
bin/install
-
Dependency Update Script (Optional):
bin/update-dependencies
These scripts ensure that the project is correctly configured with the necessary dependencies, environment variables, and Supervisor setup.
How to Use the Installation Script
Step 1: Clone the Repository
Start by cloning the repository:
git clone https://github.com/nasrulhazim/project-template.git my-project
cd my-project
Step 2: Run the Installation Script
From the project’s root directory, run the installation script:
./bin/install
What the Installation Script Does
1. Database Creation
The script extracts the project directory name and converts it to snake_case for the database name.
Example:
- Directory:
loop-tag
- Database:
loop_tag
If the database doesn’t exist, it is created using MySQL:
mysql -u<DB_USER> -p<DB_PASSWORD> -e "CREATE DATABASE loop_tag;"
2. Environment Configuration
The script updates .env.example
with:
-
APP_NAME in Title Case (e.g.,
Loop Tag
). -
DB_DATABASE in snake_case (e.g.,
loop_tag
).
It then copies .env.example
to .env
(if it doesn’t already exist) and generates an application key:
php artisan key:generate
3. Composer and npm Dependency Installation
The script installs dependencies if the required directories don’t exist:
- Composer Dependencies:
composer install
-
Update Dependencies Script:
If the
bin/update-dependencies
script exists, it runs:
bash ./bin/update-dependencies
- npm Dependencies:
npm upgrade
npm audit fix --force
npm run build
If the build modifies any files in the public/
directory, they are committed:
git add public/
git commit -m "Update public/ directory after build"
4. Supervisor Configuration
The script updates the command
and log file paths in .config/supervisord.ini
:
- Command Path Example:
command=php /var/www/loop-tag/artisan horizon
- Log File Path Example:
stdout_logfile=/var/log/supervisor/loop-tag-horizon.log
If changes are made to supervisord.ini
, they are committed:
git add .config/supervisord.ini
git commit -m "Update supervisord.ini with project-specific configurations"
5. README.md Update
The script updates README.md
with:
- The GitHub project URL (replacing
nasrulhazim/project-template
). - The project name in Title Case (e.g.,
Loop Tag
).
Example updates in README.md
:
sed -i.bak "s|nasrulhazim/project-template|<your-remote-url>|g" README.md
sed -i.bak "s|Project Template|Loop Tag|g" README.md
rm -f README.md.bak
If changes are detected, they are committed:
git add README.md
git commit -m "Update README.md to reflect project name: Loop Tag"
Troubleshooting
MySQL Database Issues:
Ensure the MySQL service is running:
sudo systemctl start mysql
Permission Issues:
Make sure the install
script has the correct permissions:
chmod +x bin/install
Supervisor Issues:
Check the Supervisor logs if Horizon doesn’t start:
tail -f /var/log/supervisor/loop-tag-horizon.log
Dependency Installation Errors:
Verify that Composer and npm are installed:
composer --version
npm --version
Conclusion
Using this installation script ensures that your Laravel project is set up consistently every time, reducing the chance of manual errors. It automates everything from environment configuration to database creation and dependency installation, giving you more time to focus on building your application.
To get started, just follow these two simple steps:
Clone the repository:
git clone https://github.com/nasrulhazim/project-template.git my-project
cd my-project
Run the installation script:
./bin/install
This script is part of the my Project Template and reflects the best practices I’ve developed through my experience working with Laravel projects.
With this automation in place, your Laravel setup becomes fast, consistent, and hassle-free.
Give it a try and enjoy a streamlined development experience!
Photo by Ruben Christen on Unsplash