February 23, 2024
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.
ufw app list
Allow OpenSSH: To ensure remote SSH access to your server remains uninterrupted, allow OpenSSH:
ufw allow OpenSSH
Enable UFW: Enable the UFW firewall. When prompted, confirm with y to proceed:
ufw enable
sudo apt update
apt --fix-broken install
When prompted, confirm with y for yes or n for no, depending on the context.
Installing Nginx: Install the Nginx web server:
sudo apt install nginx
Confirm the installation by typing y when prompted.
Configuring Firewall for Nginx: Add Nginx HTTP service to UFW and check the status:
sudo ufw allow 'Nginx HTTP'
sudo ufw status
Install MySQL Server:
sudo apt install mysql-server
Confirm with y when prompted.
sudo mysql_secure_installation
sudo mysql -u root
Then run:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ‘[NEW_PASSWORD]’;
FLUSH PRIVILEGES;
exit;
sudo add-apt-repository ppa:ondrej/php
sudo apt update
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
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
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.
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/
Check Configuration and Reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
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
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
cd /var/www/your_laravel_path
composer install
sudo nano .env
Run Laravel Optimizations:
php artisan optimize:clear
Build Your Next.js Application:
cd /var/www/your_nextjs_path
npm install
npm run build
npm install -g pm2
pm2 start npm --name "next-app" -- run start
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.
Ready to get
started ?
Tags:
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