Automating Laravel Project Setup with Custom Scripts

Nasrul Hazim Bin Mohamad - Oct 21 - - Dev Community

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:

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
Enter fullscreen mode Exit fullscreen mode

Step 2: Run the Installation Script

From the project’s root directory, run the installation script:

./bin/install
Enter fullscreen mode Exit fullscreen mode

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;"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

3. Composer and npm Dependency Installation

The script installs dependencies if the required directories don’t exist:

  • Composer Dependencies:
  composer install
Enter fullscreen mode Exit fullscreen mode
  bash ./bin/update-dependencies
Enter fullscreen mode Exit fullscreen mode
  • npm Dependencies:
  npm upgrade
  npm audit fix --force
  npm run build
Enter fullscreen mode Exit fullscreen mode

If the build modifies any files in the public/ directory, they are committed:

git add public/
git commit -m "Update public/ directory after build"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Log File Path Example:
  stdout_logfile=/var/log/supervisor/loop-tag-horizon.log
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If changes are detected, they are committed:

git add README.md
git commit -m "Update README.md to reflect project name: Loop Tag"
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

MySQL Database Issues:

Ensure the MySQL service is running:

   sudo systemctl start mysql
Enter fullscreen mode Exit fullscreen mode

Permission Issues:

Make sure the install script has the correct permissions:

   chmod +x bin/install
Enter fullscreen mode Exit fullscreen mode

Supervisor Issues:

Check the Supervisor logs if Horizon doesn’t start:

   tail -f /var/log/supervisor/loop-tag-horizon.log
Enter fullscreen mode Exit fullscreen mode

Dependency Installation Errors:

Verify that Composer and npm are installed:

   composer --version
   npm --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Run the installation script:

   ./bin/install
Enter fullscreen mode Exit fullscreen mode

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .