Introduction: From Local Build to Production Release of a C++ Application
Developing a C++ application is only half the battle. The second, equally important part is its reliable and secure deployment to a remote server (VPS). In this article, we will cover an advanced approach to deploying C++ applications that goes beyond simply copying a binary. You will learn how to set up automatic builds, run the application as a system service, organize a reverse proxy via Nginx, and implement basic monitoring.
This article is intended for developers who are already familiar with C++ and the basics of working with Linux. We will use modern tools: CMake, systemd, Nginx, and Git.
1. Build Automation and Environment Preparation on VPS
1.1. Installing Dependencies and Compiler
Before deployment, ensure that a fresh compiler (g++ or clang) and necessary libraries are installed on the server. For a production server, static linking or containerization is recommended, but within the scope of this article, we will consider the classic approach.
# Update packagessudo apt update && sudo apt upgrade -y
# Install compiler and build toolssudo apt install build-essential cmake git -y
# Install additional libraries (example for Boost)sudo apt install libboost-all-dev -y1.2. Creating a Build Script with CMake
Use CMake to manage the build. This ensures portability and simplifies integration with CI/CD. Example CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)project(MyCppApp VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Optimization for production buildset(CMAKE_BUILD_TYPE Release)
# Find external librariesfind_package(Boost REQUIRED COMPONENTS system filesystem)
# Build executableadd_executable(${PROJECT_NAME} src/main.cpp src/server.cpp)target_link_libraries(${PROJECT_NAME} PRIVATE Boost::system Boost::filesystem pthread)
# Install binary to systeminstall(TARGETS ${PROJECT_NAME} DESTINATION /usr/local/bin)After creating CMakeLists.txt, perform the build on the server:
cd /opt/myappmkdir build && cd buildcmake .. -DCMAKE_BUILD_TYPE=Releasemake -j$(nproc)sudo make install2. Configuring systemd for Process Management
Running the application in the background and automatically restarting it on failure is critically important. Use systemd to create a stable service.
2.1. Creating a Unit File
Create the file /etc/systemd/system/myapp.service:
[Unit]Description=My C++ Production ApplicationAfter=network.target
[Service]Type=simpleUser=www-dataGroup=www-dataWorkingDirectory=/opt/myappExecStart=/usr/local/bin/MyCppApp --config /etc/myapp/config.jsonRestart=alwaysRestartSec=5StandardOutput=journalStandardError=journal
# SecurityNoNewPrivileges=trueProtectSystem=fullProtectHome=true
[Install]WantedBy=multi-user.target2.2. Starting and Checking the Service
sudo systemctl daemon-reloadsudo systemctl enable myapp.servicesudo systemctl start myapp.servicesudo systemctl status myapp.serviceNow your C++ application will automatically start at system boot and restart in case of a failure.
3. Reverse Proxy via Nginx and HTTPS Configuration
Direct access to the application port is insecure and inconvenient. Use Nginx as a reverse proxy to handle static files, SSL termination, and load balancing.
3.1. Installing and Configuring Nginx
sudo apt install nginx certbot python3-certbot-nginx -yCreate the configuration /etc/nginx/sites-available/myapp:
server { listen 80; server_name myapp.example.com;
location / { pr