Introduction:
In this tutorial, we will guide you through the process of deploying a Next Js application on an Ubuntu 22.04 server using Nginx as the web server. By the end of this tutorial, you will have your Next application up and running on your Ubuntu server, accessible to users over the internet.
Prerequisites:
Before we begin, make sure you have the following prerequisites in place:
- An Ubuntu 22.04 server with a non-root user with sudo privileges.
- Node.js and npm (Node Package Manager) installed on your server.
- A Next application ready for deployment. If you don’t have one, you can create a basic Next application using the Create Next App tool.
Step 1: Install Nginx The first step is to install Nginx on your Ubuntu server. Nginx will act as the web server that serves your Next application.
- Log in to your Ubuntu server via SSH.
- Update the package lists by running the command:
sudo apt update
. - Install Nginx by running:
sudo apt install nginx
. - Start the Nginx service by running:
sudo systemctl start nginx
. - Enable Nginx to start on boot with:
sudo systemctl enable nginx
.
Step 2: Build the Next Application Next, we need to build our Next application into a production-ready bundle.
- Move to the root directory of your Next application.
- Install the required dependencies by running:
npm install
. - Build the application by running:
npm run build
. - Export the application content by :
npm run export
.
Step 3: Configure Nginx Now, we will configure Nginx to serve our Next application.
- Open the Nginx configuration file for editing:
sudo nano /etc/nginx/sites-available/default
. - Locate the
server
block and remove the existing content. - Replace it with the following configuration:
server {
listen 80;
server_name your_domain_or_server_ip;
location / {
root /path/to/your/react/application/build;
index index.html;
try_files $uri /index.html;
}
}
NginxMake sure to replace your_domain_or_server_ip with your actual domain name or server IP address, and /path/to/your/next/application/build with the absolute path to your Next application’s build folder (generated in Step 2).
Save the changes and exit the editor.
Step 4: Restart Nginx and Test the Deployment
- Restart Nginx to apply the configuration changes:
sudo systemctl restart nginx
. - Open a web browser and enter your domain name or server IP address.
- If everything is configured correctly, you should see your Next application running.
- If you encounter any issues, check the Nginx error logs for troubleshooting:
sudo tail -f /var/log/nginx/error.log
.
Step 5: Automate Deployment (Optional) To simplify the deployment process, you can automate it using a deployment script or a CI/CD (Continuous Integration/Continuous Deployment) tool like Jenkins or GitLab CI/CD.
Conclusion:
Congratulations! You have successfully deployed your Next application on an Ubuntu 22.04 server using Nginx as the web server. You can now access your Next application from any web browser. Feel free to explore further customisation options and optimisation based on your specific requirements. Happy coding!