Deploying a C++ Application on VPS: A Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Deploying a C++ Application on VPS: A Step-by-Step Guide



Developing in C++ is only half the battle. To make your application accessible to users, it needs to be deployed on a server. In this article, we will thoroughly examine the process of deploying a C++ application on a VPS (Virtual Private Server) running Linux. You will learn how to prepare the server, compile the code, configure auto-start, and ensure security. The material is intended for developers familiar with the basics of C++ and Linux.



1. Preparing the VPS and Installing Necessary Tools



Before uploading your code, make sure your VPS is ready for work. We will use Ubuntu 22.04 LTS (or newer) — the most stable and popular distribution for web servers.



1.1. Connecting to the Server



After purchasing a VPS, you will receive an IP address, a username (usually root), and a password. Connect via SSH:



ssh root@your_server_IP


For security, it is recommended to create a separate user and disable root login, but for the first deployment, you can work as root.



1.2. Updating the System and Installing the Compiler



Let's update the package manager and install the necessary tools for building C++:



apt update && apt upgrade -yapt install build-essential cmake git -y


  • build-essential — a set of compilers (g++, gcc) and utilities.
  • cmake — a build system that simplifies compilation.
  • git — for cloning the repository.


Verify the installation:



g++ --versioncmake --version


2. Compiling and Building the C++ Application



Let's assume your application is in a Git repository. We will clone it to the server and build it.



2.1. Cloning the Repository



cd /optgit clone https://github.com/your_username/your_application.gitcd your_application


It is recommended to place projects in the /opt or /var/www directory, rather than in the home folder.



2.2. Building with CMake



Let's create a separate directory for the build and run CMake:



mkdir build && cd buildcmake ..make -j$(nproc)


The -j$(nproc) flag speeds up compilation by using all CPU cores. After a successful build, the executable file will appear in the build folder.



2.3. Testing the Application



Run the application manually to make sure it works:



./your_application


If the application is an HTTP server, test it with curl:



curl http://localhost:8080


3. Configuring Auto-Start with systemd



To ensure the application starts automatically when the server boots and restarts in case of a crash, we will create a systemd service.



3.1. Creating the Unit File



Create the file /etc/systemd/system/myapp.service:



nano /etc/systemd/system/myapp.service


Insert the following content (replace the paths and username):



[Unit]Description=My C++ ApplicationAfter=network.target

[Service]ExecStart=/opt/your_application/build/your_applicationWorkingDirectory=/opt/your_application/buildRestart=alwaysRestartSec=5User=www-dataGroup=www-data

[Install]WantedBy=multi-user.target


  • ExecStart — the full path to the executable file.
  • Restart=always — automatic restart on crash.
  • User/Group — it is recommended to use an unprivileged user (e.g., www-data).


3.2. Starting the Service



systemctl daemon-reloadsystemctl enable myapp.servicesystemctl start myapp.service


Check the status:



sys                

Blogs

Book Recommendations