Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Deploying Full-Stack Applications on CentOS Server

Tech May 15 1

Preparation Steps

Before deploying your application, you'll need to prepare your CentOS server and development environment.

Required Tools

Download and install WinSCP for file transfer between your local machine and the server. This secure file transfer protocol client allows you to easily move files to your server environment.

Server Connection

Establissh a connection to your CentOS server using your preferred SSH client. Ensure you have administrative privileges for the deployment process.

Database Setup

Install and configure MySQL for your application's data storage needs.

Option 1: Native MySQL Installation

Download the MySQL 8.x Linux binary package from the official MySQL website. Transfer the downloaded file to your server using WinSCP.

Extracting MySQL

tar -xvf mysql-8.3.0-linux-glibc2.28-x86_64.tar.xz -C /usr/local
  • -x: Extract the archive
  • -v: Verbose mode (show detailed output)
  • -f: Specify the archive file
  • -C: Set the target directory

Configuration

cd /usr/local/
mv mysql-8.3.0-linux-glibc2.28-x86_64 mysql

Starting MySQL Service

cd /usr/local/mysql/support-files
./mysql.server start

Option 2: Docker MySQL

For a containerized approach, use Docker to deploy MySQL:

Pull MySQL Image

docker pull mysql:8.0

Create MySQL Container

docker run --name db-container -dp 3306:3306 -e MYSQL_ROOT_PASSWORD=your_password -d mysql

Create Database

docker exec -it db-container mysql -u root -p
CREATE DATABASE your_database;

Import SQL Data

Since containers have isolated filesystems, you need to copy your SQL file to the container first:

# Copy SQL file to container
docker cp /path/to/your/data.sql db-container:/data.sql

# Enter container and import data
docker exec -it db-container bash
mysql -u root -p your_database < /data.sql

Backend Deployment

Deploy your backend application using Docker for consistent environments.

Create Dockerfile

In your backend project root directory, create a Dockerfile:

# Use Java 17 runtime environment
FROM openjdk:17-jdk

# Set the working directory
WORKDIR /app

# Copy the JAR file
ARG JAR_FILE
COPY your-backend.jar app.jar

# Set timezone to Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone

# Expose the application port
EXPOSE 8081

# Start the application
ENTRYPOINT ["java", "-jar", "/app.jar"]

Build and Run Backend

# Build Docker image
docker build -t backend-service .

# Run the container
docker run -p 8081:8081 --name backend-container -d backend-service

# Check logs
docker logs backend-container

Troubleshooting

  • Java not found error: Ensure you're using the correct base image (FROM openjdk:17-jdk)
  • JAR file not found error: Verify the correct path to your JAR file in the Dockerfile

Frontend Deployment

Deploy your frontend application using either Docker or OpenResty.

Option 1: Docker Deployment

Create a Dockerfile in your frontend project root:

FROM nginx:alpine

# Copy frontend files to nginx default directory
COPY . /usr/share/nginx/html

# Expose port 80
EXPOSE 80

# Start nginx
CMD ["nginx", "-g", "daemon off;"]

Build and Run Frontend with Docker

# Build Docker image
docker build -t frontend-app .

# Run the container
docker run -dp 80:80 --name frontend-container frontend-app

Option 2: OpenResty Deployment

For production environments, OpenResty (extended Nginx) provides better performance:

Install OpenResty

# Add OpenResty repository
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

# Install OpenResty
yum install openresty

Deploy Frontend Files

# Copy your built frontend files to OpenResty directory
cp -r * /usr/local/openresty/nginx/html/

# Start OpenResty
/usr/local/openresty/nginx/sbin/nginx

Stop OpenResty

/usr/local/openresty/nginx/sbin/nginx -s stop

Reverse Proxy Configuration

Configure OpenResty to proxy API requests to your backend service:

# Edit /usr/local/openresty/nginx/conf/nginx.conf

server {
    listen 80;
    server_name your_domain.com;

    location /api/ {
        proxy_pass http://your_backend_ip:8081/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /uploads/ {
        proxy_pass http://your_backend_ip:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location / {
        root /usr/local/openresty/nginx/html;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

After making configuration changes, restart OpenResty:

/usr/local/openresty/nginx/sbin/nginx -s reload

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.