nest-admin

repository·master·Indexed 21 days ago

https://github.com/taozhi1010/nest-admin

A comprehensive backend management system built on NestJS (version 3.8.7). It features RBAC, system monitoring, and automated code generation. The project includes a NestJS server and frontend options, with a current focus on the Admin-vue3 version as the Vue2 version has suspended maintenance.

Tokens
35.9K
Snippets
164
Records
182
Agent score
75%

What's inside nest-admin

  1. Overview of Nest-Admin built-in features

    master

    Nest-Admin is an out-of-the-box mid-to-back-office management system built on the latest version of NestJS. It includes the following core modules:

    • User & Access Management: User management, Department management (tree structure with data permissions), Post management, Menu management (permissions and button identifiers), and Role management (menu and data scope permissions).
    • System Configuration: Dictionary management, Parameter management, and Scheduled Tasks (with execution logs).
    • Monitoring & Logs: Operation logs, Login logs, Online user monitoring, Service monitoring (CPU, memory, disk, stack), Cache monitoring, and Connection pool monitoring (SQL analysis).
    • Development Tools: Code generation (Node, HTML, SQL, and CRUD downloads), Automatic API documentation generation, and an Online Builder (drag-and-drop HTML form generation).
  2. Set up the nest-admin development environment

    master

    To set up the local development environment, clone the repository, navigate to the admin directory, install dependencies using npm, and start the development server.

    Note on dependency installation: It is recommended not to use cnpm directly as it may cause various bugs. If you experience slow download speeds with npm, use the official npm mirror instead.

    # Clone the project
    git clone https://github.com/taozhi1010/nest-admin
    
    # Enter the project directory
    cd admin
    
    # Install dependencies (use the mirror if npm is slow)
    npm install --registry=https://registry.npmmirror.com
    
    # Start the development service
    npm run dev
  3. Install Node.js environment on Linux

    master

    To deploy the NestJS backend, you must first install Node.js manually and create symbolic links to make the binaries globally accessible. It is recommended to use a stable version like Node 20.

    1. Create a directory for the Node.js installation.
    2. Download the Node.js package (e.g., using wget).
    3. Extract the package to your chosen directory.
    4. Create symbolic links for node, npm, and npx in /usr/bin/ so they can be used by all users.
    # 1. Create directory
    mkdir -p /server/soft/node
    cd /server/soft/node
    
    # 2. Download Node.js (example version v20.5.0)
    wget https://nodejs.org/dist/v20.5.0/node-v20.5.0-linux-x64.tar.xz
    
    # 3. Extract
    sudo tar -xJvf node-v20.5.0-linux-x64.tar.xz -C /server/soft/node
    
    # 4. Create symbolic links
    sudo ln -s /server/soft/node/node-v20.5.0-linux-x64/bin/node /usr/bin/node
    sudo ln -s /server/soft/node/node-v20.5.0-linux-x64/bin/npm /usr/bin/npm
    sudo ln -s /server/soft/node/node-v20.5.0-linux-x64/bin/npx /usr/bin/npx
    
    # 5. Verify
    node -v
    npm -v
  4. Install and start Nginx on OpenCloudOS/CentOS

    master

    To deploy Nginx on an OpenCloudOS or CentOS-compatible system using the yum package manager, follow these steps:

    1. Update system packages: Ensure your package list is current.
    2. Install EPEL repository: Required for many additional packages.
    3. Install Nginx: Use yum to download and install the service.
    4. Start and Enable Nginx: Start the service immediately and set it to launch automatically on system boot.
    5. Verify status: Check if the service is active (running).

    Note: If you cannot access the Nginx welcome page via your server's public IP, ensure your system firewall is configured to allow HTTP traffic.

    # Update system
    sudo yum update
    
    # Install EPEL repository
    sudo yum install epel-release
    
    # Install Nginx
    sudo yum install nginx
    
    # Start Nginx
    sudo systemctl start nginx
    
    # Enable auto-start on boot
    sudo systemctl enable nginx
    
    # Check status
    sudo systemctl status nginx
  5. Install and run Nest-Admin locally

    master

    To set up a local development environment for Nest-Admin, clone the repository, install dependencies using yarn, and start the development server.

    Note: This version (Vue 2) is currently under maintenance pause. For the Vue 3 frontend version, please refer to the Admin-vue3 repository.

    # Clone the repository
    $ git clone git@github.com:taozhi1010/nest-admin.git
    
    # Install dependencies
    $ cd nest-admin && yarn
    
    # Run in development mode
    $ yarn start:dev
  6. Deploy and start the NestJS project with PM2

    master

    Follow these steps to deploy the backend:

    1. Upload your NestJS project as a compressed tarball (e.g., nest-server.tar).
    2. Extract the tarball to the target directory.
    3. Install dependencies using npm install within the project's server directory.
    4. Start the project using PM2, pointing to the compiled main.js file in the dist folder.

    Note: Ensure your firewall (e.g., Tencent Cloud console) allows traffic on the port your application is listening on (e.g., 8080).

    # 1. Create directory and extract
    mkdir -p /server/soft/nest-admin
    sudo tar -xvf nest-server.tar -C /server/soft/nest-admin
    
    # 2. Install dependencies
    cd /server/soft/nest-admin/server/
    npm install
    
    # 3. Start with PM2
    cd dist
    pm2 start main.js
    
    # Access via: http://<IP_ADDRESS>:8080/swagger-ui/
  7. Build for staging or production environments

    master

    Use the following commands to build the project for different deployment environments:

    • Staging: Use npm run build:stage to create a build for the testing/staging environment.
    • Production: Use npm run build:prod to create a build for the production environment.
    # Build for staging environment
    npm run build:stage
    
    # Build for production environment
    npm run build:prod
  8. Configure Nginx for Single Page Applications (SPA)

    master

    When deploying frontend projects (like those built with Vite) that use history mode for routing, you must configure the try_files directive. Without this, refreshing a page on a sub-route will result in a 404 error because Nginx tries to find a physical file at that path instead of letting the frontend router handle it.

    Key Configuration Steps:

    1. Set the root to your project's distribution directory (e.g., /server/website/dist).
    2. Use try_files $uri $uri/ /index.html; to redirect all non-file requests to the entry point.
    3. After modifying the configuration, restart Nginx using sudo systemctl restart nginx.

    Important Frontend Requirement: Ensure your frontend environment variable VITE_APP_BASE_API is correctly set to your backend API path to avoid proxying requests to the frontend directory.

    server {
        listen       80;
        server_name  admin.crazystudent13.cn;
    
        location / {
            root /server/website/dist;
            try_files $uri $uri/ /index.html;
            index index.html;
        }
    
        error_page 404 /404.html;
        location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
  9. Configure firewall to allow Redis traffic

    master

    To allow external clients to connect to Redis, you must open the default port 6379 in your system firewall. This guide uses ufw on Red Hat-based systems like OpenCloudOS.

    1. Install UFW: Use yum to install and enable the Uncomplicated Firewall.
    2. Open Port: Allow TCP traffic on port 6379.

    Note: If you are using a cloud provider (like Tencent Cloud), you may also need to open the port in their web console firewall settings.

    # Install and enable UFW
    yum update
    yum install ufw
    yum enable ufw
    
    # Allow Redis port
    ufw allow 6379/tcp
  10. Build for staging and production environments

    master

    Use the following commands to build the project for different deployment environments:

    • Staging/Test environment: npm run build:stage
    • Production environment: npm run build:prod
    # Build for staging environment
    npm run build:stage
    
    # Build for production environment
    npm run build:prod
  11. Install and configure PM2 for process management

    master

    PM2 is used to manage the NestJS application processes on the server. After installing Node.js, install PM2 globally via npm and create a symbolic link to /usr/bin/pm2 to ensure it is available in the system path.

    # Install PM2 globally
    npm install pm2 -g
    
    # Create symbolic link
    sudo ln -s /server/soft/node/node-v20.5.0-linux-x64/bin/pm2 /usr/bin/pm2