Are you planning to host a Laravel PHP application on your Ubuntu server? To ensure a seamless, high-performance environment, it’s essential to use modern technologies like Nginx, PHP 8.3, Node.js, and Certbot for SSL. In this guide, I’ll walk you through the complete setup process, from installing the required software to configuring your server for optimal performance.
This tutorial is perfect for developers and website owners who want a secure, scalable, and efficient hosting environment for their Laravel applications. By following these steps, you’ll not only create a stable hosting setup but also improve your website’s speed, security, and SEO rankings. Here’s what we’ll cover:
- Installing and configuring Nginx as the web server.
- Setting up PHP-FPM (v8.3) for dynamic content processing.
- Installing Node.js with NVM for asset building and runtime dependencies.
- Using Certbot to secure your website with SSL.
- Optimizing server settings for Laravel.
Whether you’re setting up a new Laravel application or migrating an existing one, this guide has got you covered. Let’s dive in and turn your Ubuntu server into a robust Laravel hosting powerhouse!
Here is the comprehensive step-by-step guide for setting up an Nginx server with PHP-FPM 8.3, Node.js, and Certbot on Ubuntu to host a Laravel PHP application:
Step 1: Update and Upgrade the System
sudo apt update && sudo apt upgrade -y
Step 2: Install PHP-FPM 8.3 and Required Extensions
# Add the required repository for PHP 8.3 sudo add-apt-repository ppa:ondrej/php -y sudo apt update # Install PHP 8.3 and necessary extensions sudo apt install php8.3 php8.3-fpm php8.3-mbstring php8.3-xml php8.3-bcmath php8.3-curl php8.3-mysql php8.3-zip unzip -y # Enable PHP-FPM service sudo systemctl enable php8.3-fpm sudo systemctl start php8.3-fpm
Step 3: Install and Configure Nginx
sudo apt install nginx -y # Enable and start Nginx service sudo systemctl enable nginx sudo systemctl start nginx
Create an Nginx Configuration for Laravel
sudo nano /etc/nginx/sites-available/laravel
Paste the following configuration (update the domain and paths as needed):
server { listen 80; server_name example.com www.example.com; root /var/www/laravel/public; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } client_max_body_size 100M; }
Link the configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/ sudo nginx -t # Test the configuration sudo systemctl reload nginx
Step 4: Install Node.js using NVM
# Install NVM (Node Version Manager) curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash # Restart the terminal or source NVM source ~/.bashrc # Install Node.js v20 nvm install 20 # Verify Node.js and npm versions node -v npm -v
Step 5: Set Up Certbot for SSL
# Install Certbot and Certbot Nginx plugin sudo apt install certbot python3-certbot-nginx -y # Obtain an SSL certificate for your domain sudo certbot --nginx -d example.com -d www.example.com # Verify the certificate renewal process sudo certbot renew --dry-run
Automate Certificate Renewal
Edit the crontab:
sudo crontab -e
Add this line to automatically renew certificates and reload Nginx:
0 0,12 * * * certbot renew --post-hook "systemctl reload nginx"
Step 6: Set Permissions for Laravel
sudo chown -R www-data:www-data /var/www/laravel sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache
Final Checks
- Verify Nginx status:
sudo systemctl status nginx
- Verify PHP-FPM status:
sudo systemctl status php8.3-fpm
- Test your site in a browser to ensure it’s working over HTTPS.
Your Laravel application is now hosted with a secure setup.
Next Connecting GIT
Here are the steps to install Git on your server and connect it to a repository for seamless deployment:
Step 1: Install Git on Ubuntu
sudo apt update sudo apt install git -y
Verify the installation:
git --version
Step 2: Configure Git on the Server
Set up your Git username and email for commits made on the server:
git config --global user.name "Your Name" git config --global user.email "your-email@example.com"
Verify the configuration:
git config --list
Step 3: Generate an SSH Key for Repository Access
To connect your server to a remote repository, you’ll need an SSH key:
- Generate an SSH key pair:
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
- Press Enter to save the key in the default location (
/home/your_user/.ssh/id_rsa
). - Optionally set a passphrase for added security.
- Press Enter to save the key in the default location (
- Copy the public key:
cat ~/.ssh/id_rsa.pub
- Add the SSH key to your Git hosting service:
- For GitHub: Go to Settings → SSH and GPG Keys → New SSH Key, and paste the public key.
- For GitLab: Go to Settings → SSH Keys, and add the key.
- For Bitbucket: Go to Personal Settings → SSH Keys, and add the key.
Step 4: Test the Connection
Verify that the server can connect to your repository:
ssh -T git@github.com
For GitLab or Bitbucket, replace git@github.com
with git@gitlab.com
or git@bitbucket.org
.
You should see a message like:
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
Step 5: Clone the Repository
Navigate to the directory where you want to clone your Laravel project:
cd /var/www
Clone your repository:
git clone git@github.com:your-username/your-repository.git laravel
Replace your-username
and your-repository
with your GitHub username and repository name.
Step 6: Set Up Permissions
After cloning the repository, set appropriate permissions for Laravel:
sudo chown -R www-data:www-data /var/www/laravel sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache
Step 7: Automate Deployment (Optional)
For easier updates, you can pull the latest code from your repository directly on the server:
cd /var/www/laravel git pull origin main
To streamline this process, consider setting up a deployment script or a Continuous Deployment (CD) tool like GitHub Actions, GitLab CI/CD, or Bitbucket Pipelines.
With these steps, you’ve now installed Git and connected your server to your repository. This setup ensures that deploying updates is quick, secure, and hassle-free.
And Finally Connecting to RDS
To connect your Laravel application to an Amazon RDS database, follow these steps:
Step 1: Gather Your RDS Instance Details
Log in to the AWS Management Console and navigate to the RDS service to gather the following details about your database instance:
- Endpoint (e.g.,
mydb-instance.123456789012.us-east-1.rds.amazonaws.com
) - Database name
- Username
- Password
- Port (default is
3306
for MySQL)
Step 2: Update Security Group Settings
To allow your Laravel server to connect to the RDS instance, update the RDS security group:
- Go to the EC2 Dashboard → Security Groups.
- Find the security group attached to your RDS instance.
- Add an inbound rule to allow traffic from your Laravel server:
- Type: MySQL/Aurora
- Protocol: TCP
- Port: 3306
- Source: IP of your Laravel server or its security group.
Step 3: Install the MySQL Client on Your Server
Ensure that the required MySQL client libraries are installed on your Laravel server:
sudo apt install mysql-client -y
Test the connection to the RDS instance (optional):
mysql -h <RDS_ENDPOINT> -u <USERNAME> -p
Replace <RDS_ENDPOINT>
and <USERNAME>
with your RDS endpoint and username. Enter your password when prompted.
Step 4: Configure Laravel’s .env
File
Edit your Laravel .env
file to include the RDS database configuration:
DB_CONNECTION=mysql
DB_HOST=mydb-instance.123456789012.us-east-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Step 5: Test the Database Connection
Run the following command to test the database connection:
php artisan migrate
If the migration runs successfully, the connection is working.
Discover more from AJB Blog
Subscribe to get the latest posts sent to your email.