February 23, 2024

How to Deploy a Laravel and Next.js Application on Ubuntu with Nginx

Deploy Laravel & Next.js on Ubuntu with Nginx: A complete guide covering server setup, PHP, MySQL, Composer, and Node.js for robust web deployments.


Deploying Laravel and Next.js applications on an Ubuntu server involves several steps, from setting up the server environment to configuring Nginx to serve your applications. This guide walks you through the entire process, ensuring your applications are securely deployed and accessible.

 

Preparing the Server Environment

Setting Up the Firewall with UFW

 

  1. List Available Applications: Start by listing applications that UFW knows how to work with:
    ufw app list
    
  2. Allow OpenSSH: To ensure remote SSH access to your server remains uninterrupted, allow OpenSSH:

    ufw allow OpenSSH
    
  3. Enable UFW: Enable the UFW firewall. When prompted, confirm with y to proceed:

    ufw enable
    

Installing Essential Packages

 

  1. Update Your Package List: Ensure your package list and existing packages are up to date: 
    sudo apt update
    apt --fix-broken install
    

    When prompted, confirm with y for yes or n for no, depending on the context.

     

  2. Installing Nginx: Install the Nginx web server:

    sudo apt install nginx
    

    Confirm the installation by typing y when prompted.

     

  3.  Configuring Firewall for Nginx: Add Nginx HTTP service to UFW and check the status:

    sudo ufw allow 'Nginx HTTP'
    sudo ufw status
    

Setting Up MySQL

 

  1. Install MySQL Server:

    sudo apt install mysql-server
    

    Confirm with y when prompted.

  2. Secure MySQL Installation: Run the mysql_secure_installation script to set up security for your MySQL server. Follow the prompts to set a password for the MySQL root user and to remove insecure default settings:
    sudo mysql_secure_installation
    
  3. Changing MySQL Root Password (Optional): If necessary, change the root password:
    sudo mysql -u root
    

    Then run:

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ‘[NEW_PASSWORD]’;
    FLUSH PRIVILEGES;
    exit;
    

Installing PHP

 

  1. Add PHP Repository: To install the latest PHP version, add the Ondřej Surý's PHP repository:
    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    
  2. Install PHP 8.2 and required extensions:
    sudo apt install php8.2
    sudo apt install php8.2-{bcmath,fpm,xml,mysql,zip,intl,ldap,gd,cli,bz2,curl,mbstring,pgsql,opcache,soap,cgi}
    

    Confirm with y when prompted. Restart Nginx to apply the changes:

    systemctl restart nginx.service
    

     

Deploying Applications

Setting Up Your Applications

 

  1. Clone Your Repositories: Clone your Laravel and Next.js applications from GitHub or another source into /var/www:
    cd /var/www
    git clone [YOUR_GITHUB_URL]
    

    Adjust the ownership of the cloned repository: 

    sudo chown -R $USER:$USER /var/www/your_repo_path
    

Configuring Nginx

 

  1. Configure Nginx for Your Applications: Create and edit Nginx server block files for both your Laravel and Next.js applications. Replace your_nextjs_domain.com and your_laravel_domain.com with your actual domain names.

    • For Next.js:
      sudo nano /etc/nginx/sites-available/your_nextjs_domain.com
      

      Insert the server block configuration for proxying requests to your Next.js app.

    • For Laravel:

      sudo nano /etc/nginx/sites-available/your_laravel_domain.com
      

      Insert the server block configuration for serving your Laravel app.

  2. Enable the Server Blocks: Create symbolic links to enable the configurations:

    sudo ln -s /etc/nginx/sites-available/your_nextjs_domain.com /etc/nginx/sites-enabled/
    sudo ln -s /etc/nginx/sites-available/your_laravel_domain.com /etc/nginx/sites-enabled/
    
  3. Check Configuration and Reload Nginx:

    sudo nginx -t
    sudo systemctl reload nginx
    

Installing Composer and Node.js

 

  1. Install Composer: Download and install Composer globally:

    curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
    HASH=`curl -sS https://composer.github.io/installer.sig`
    php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
    

     

  2.  Install Node.js: Add NodeSource repository and install Node.js (adjust the version as needed):
    curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

Setting Up Your Laravel Application

 

  1. Install Laravel Dependencies: 
    cd /var/www/your_laravel_path
    composer install
    
  2. Configure .env File: Ensure your .env file is correctly set up for your environment. Use nano or another text editor to edit it:
    sudo nano .env
    
  3. Run Laravel Optimizations:

    php artisan optimize:clear
    

Setting Up Your Next.js Application

 

  1. Build Your Next.js Application:

    cd /var/www/your_nextjs_path
    npm install
    npm run build
    
  2.  Use PM2 to Manage Your Next.js Process: Install PM2 and start your Next.js application:
    npm install -g pm2
    pm2 start npm --name "next-app" -- run start
    

 

Conclusion

You've now set up your Ubuntu server with Nginx to serve both a Laravel and a Next.js application, secured your MySQL installation, installed PHP with necessary extensions, and set up Composer and Node.js. This setup provides a robust environment for deploying modern web applications.



Tags:

Deploy Laravel UbuntuDeploy Next.js UbuntuNginx Configuration GuideUbuntu Server Setup for Web AppsPHP 8.2 Installation UbuntuSecure MySQL InstallationCom

Start building now

Sign up now, and you'll be swiftly up and running on Hostnserver.com in just a matter of minutes.

Create your account

© 2024 Hostnserver.com | All rights reserved