# How to deploy a nextjs server project on your own server


By [银河42](https://paragraph.com/@42-8) · 2023-12-04

---

Deploying a Next.js server project on your own server involves several steps, from preparing the server environment to configuring a reverse proxy for better performance and security. Here's a detailed guide:

### **1\. Prepare Your Server**

*   **Operating System**: Ensure your server has an appropriate operating system installed, like Ubuntu, CentOS, or any other Linux distribution.
    
*   **Node.js**: Next.js is a Node.js framework, so you need to have Node.js installed. You can use nvm (Node Version Manager) to install the latest stable version of Node.js.
    
*   **npm or Yarn**: These are package managers for Node.js. They are used to manage and install project dependencies.
    
*   **Git**: To clone your project repository to your server, Git must be installed.
    

### **2\. Secure Your Server**

**SSH Access**: Make sure you can securely access your server via SSH. Firewall Configuration: Open the necessary ports, typically port 80 for HTTP and 443 for **HTTPS. SSL Certificate**: If you plan to use HTTPS, consider setting up an SSL certificate using Let's Encrypt for free SSL certificates.

### **3\. Clone Your Project**

Clone your Next.js project repository on the server. Navigate to the desired directory and use:

`git clone [your-repository-url] cd [your-project-directory]`

### **4\. Install Dependencies**

Inside your project directory, install all necessary dependencies:

`npm install`

or

`yarn install`

### **5\. Build the Project**

Build your Next.js project:

`npm run build`

`or`

`yarn build`

This will create an optimized production build of your project.

### **6\. Environment Variables**

If your project uses environment variables, make sure to set them up on your server. You can do this by creating a `.env` file in your project directory or setting them directly in your server environment.

### **7\. Start the Application**

Start your Next.js application:

`npm start`

`yarn start`

This will typically start your application on port 3000.

### **8\. Process Manager**

Use a process manager like PM2 to keep your application running in the background and restart it automatically if it crashes:

`npm install pm2 -g pm2 start npm --name "next-app" -- start pm2 save pm2 startup`

### **9\. Set Up a Reverse Proxy**

*   Configure a reverse proxy server like Nginx or Apache to forward requests to your Next.js app. This enables your app to be accessed on standard ports (80, 443) and to utilize SSL.
    
*   Here’s an example configuration for Nginx:
    
*   `server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }`
    

### **10\. Monitoring and Maintenance**

*   Regularly monitor the performance of your server and application.
    
*   Keep your server's operating system and software packages updated.
    
*   Set up a backup process for your application and its data.
    

### **Additional Notes**

*   **Security**: Always prioritize server security. Regularly check and apply security patches.
    
*   **Performance Tuning**: Depending on your application's traffic and performance requirements, you might need to adjust server resources.
    
*   **Backup**: Regularly back up your application data and server configuration.
    

The deployment process may vary based on your specific server setup and needs, so you might need to adjust these steps accordingly.

---

*Originally published on [银河42](https://paragraph.com/@42-8/how-to-deploy-a-nextjs-server-project-on-your-own-server)*
