Deploying a C++ Application on VPS: Step-by-Step Guide
C++ development often ends with the deployment stage — transferring the finished application to a remote server (VPS). Unlike interpreted languages, C++ requires compilation for the target architecture, proper environment configuration, and setting up autostart. In this article, we will cover the full cycle: from VPS preparation to launching a production version of a C++ application using systemd and Nginx.
1. VPS Preparation and Installing Necessary Tools
Before deployment, make sure your VPS is running Ubuntu 20.04/22.04 or Debian 11/12. Connect via SSH:
ssh root@your-server-ipUpdate the package database and install the g++ compiler, CMake build system, and screen process manager (for debugging purposes):
apt update && apt upgrade -yapt install build-essential cmake screen git -yCheck the compiler version:
g++ --versionIf you use libraries (e.g., Boost, OpenSSL), install them in advance:
apt install libboost-all-dev libssl-dev -y2. Compiling the C++ Application for the Server Environment
Transfer the source code to the VPS. The easiest way is via Git:
git clone https://github.com/your-repository/project.gitcd projectCreate a build directory and perform compilation with production optimizations:
mkdir build && cd buildcmake .. -DCMAKE_BUILD_TYPE=Releasemake -j$(nproc)The -j$(nproc) flag speeds up the build by using all CPU cores. After successful compilation, an executable file will appear (e.g., app). Check its functionality:
./appIf the application uses configuration files or resources, copy them to the folder next to the binary or configure the paths in the code via argv.
3. Setting Up Autostart via systemd
To make the application start at system boot and automatically restart on failures, create a systemd service.
Create the file /etc/systemd/system/myapp.service:
nano /etc/systemd/system/myapp.serviceInsert the following content (replace the paths with your own):
[Unit]Description=My C++ ApplicationAfter=network.target
[Service]ExecStart=/root/project/build/appWorkingDirectory=/root/project/buildRestart=alwaysRestartSec=5User=rootGroup=rootEnvironment="APP_ENV=production"
[Install]WantedBy=multi-user.targetExplanation of parameters:
Restart=always— restart on any exit (except stopping via systemctl stop).RestartSec=5— pause of 5 seconds before restarting.Environment— passing environment variables (e.g., for configuration).
Activate and start the service:
systemctl daemon-reloadsystemctl enable myappsystemctl start myappCheck the status:
systemctl status myappYou should see active (running). If the application crashes — systemd will automatically restart it after 5 seconds.
4. Setting Up Nginx as a Reverse Proxy (Optional)
If your C++ application listens on HTTP (e.g., a REST API on port 8080), it is useful to put Nginx in front of it for SSL termination, load balancing, and static file serving.
Install Nginx:
apt install nginx -yCreate a configuration for your domain (or IP) in /etc/nginx/sites-available/myapp:
server { listen 80; server_name your-domain.com;
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header H