Automating Laravel Project Setup with Nginx and PHP-FPM: A Simple Bash Script

Automating Laravel Project Setup with Nginx and PHP-FPM: A Simple Bash Script

Intro

When setting up a Laravel project with Nginx and PHP-FPM, there are several steps to follow and configuration files to modify. This process can become tedious and time-consuming, especially when you need to do it frequently. To make the process quicker and less error-prone, I created a simple bash script that automates the entire setup process. In this blog post, I will share this script with you, explain how it works, and how to use it to setup a Laravel project with Nginx and PHP-FPM.

Prerequisites

To make the best use of this script, you should have the following:

  1. A Linux-based system (preferably Ubuntu) with root access.
  2. Nginx installed and configured.
  3. PHP-FPM installed and configured.
  4. A basic understanding of bash scripting, Nginx, and PHP-FPM.
This script is designed for those who have a working Nginx and PHP-FPM setup and want to setup Laravel projects quickly and efficiently.

The Script

Here is the complete script:

The script takes three parameters:

  1. -p: The PHP version you want to use with the Laravel project (e.g., 7.4 or 8.0).
  2. -n: The name of your Laravel project (e.g., my_project).
  3. -d: The root domain name pointing to the server (e.g., example.com).
An example of running the script:

Attention: The provided script is for educational and informational purposes only. While the author has made every effort to ensure its accuracy and effectiveness, users are advised to use the script at their own risk. The author assumes no responsibility for any damages, data loss, or issues that may arise from using this script. Always make sure to backup your data and test the script in a safe environment before deploying it in a production system.

Explanation

The script does the following:

  1. It parses the provided command-line arguments to extract the PHP version, project name, and domain name.
  2. It creates a new directory for the Laravel project in the /var/www/html/ folder.
  3. It changes the ownership of the project folder to the current user (ubuntu) and the www-data group.
  4. It creates a new Nginx configuration file for the project in the /etc/nginx/sites-available/ directory.
  5. It populates the Nginx configuration file with the necessary settings for the Laravel project, using the provided PHP version, project name, and domain name.
  6. It creates a symbolic link to the configuration file in the /etc/nginx/sites-enabled/ directory.
  7. It restarts the PHP-FPM and Nginx services to apply the changes.

Conclusion

This bash script simplifies the process of setting up Laravel projects with Nginx and PHP-FPM. Remember to modify the script as needed to fit your specific environment and requirements. Happy coding!