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

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

Introduction: Why Deploy a C++ Application on a VPS?

\n

C++ remains one of the most performant programming languages. If you have created a high-load server, parser, game engine, or microservice in C++, sooner or later the question of publishing it on the internet will arise. Renting a VPS (Virtual Private Server) is the optimal choice for such tasks: you get full control over the environment, which is critical for compiled languages.

\n

In this article, we will cover the complete cycle: from setting up an empty server to launching a C++ binary under systemd and publishing it via Nginx. All code examples are functional and tested on Ubuntu 22.04.

\n\n

1. VPS Preparation and Tool Installation

\n\n

1.1. Connecting to the Server

\n

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

\n
ssh root@your_server_IP
\n

We recommend immediately updating the package database and installing basic utilities:

\n
apt update && apt upgrade -y\napt install git curl wget build-essential -y
\n\n

1.2. Installing the Compiler and CMake

\n

To build a C++ project, you will need g++ and cmake:

\n
apt install g++ cmake -y\ng++ --version  # Check version
\n

If you need a newer version of GCC (e.g., for C++20), add the PPA:

\n
add-apt-repository ppa:ubuntu-toolchain-r/test -y\napt install g++-13 -y\nupdate-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 100
\n\n

2. Writing a Test C++ Application (HTTP Server)

\n

For demonstration, we will create a simple HTTP server in C++ using sockets. It will listen on port 8080 and respond with "Hello from C++ VPS!".

\n\n

2.1. Project Structure

\n
mkdir ~/cpp-server && cd ~/cpp-server\nnano main.cpp
\n\n

2.2. Source Code (main.cpp)

\n
#include <iostream>\n#include <string>\n#include <sstream>\n#include <sys/socket.h>\n#include <netinet/in.h>\n#include <unistd.h>\n\nint main() {\n    int server_fd, new_socket;\n    struct sockaddr_in address;\n    int opt = 1;\n    int addrlen = sizeof(address);\n    char buffer[1024] = {0};\n\n    // Create socket\n    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {\n        perror("socket failed");\n        exit(EXIT_FAILURE);\n    }\n\n    // Bind port\n    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {\n        perror("setsockopt");\n        exit(EXIT_FAILURE);\n    }\n    address.sin_family = AF_INET;\n    address.sin_addr.s_addr = INADDR_ANY;\n    address.sin_port = htons(8080);\n\n    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {\n        perror("bind failed");\n        exit(EXIT_FAILURE);\n    }\n\n    if (listen(server_fd, 3) < 0) {\n        perror("listen");\n        exit(EXIT_FAILURE);\n    }\n\n    std::cout << "Server listening on port 8080..." << std::endl;\n\n    while (true) {\n        if ((new_socket = accept(server_fd, (struct sockaddr *)&address,\n                                 (socklen_t*)&addrlen)) < 0) {\n            perror("accept");\n            continue;\n        }\n\n        read(new_socket, buffer, 1024);\n        std::string response = "HTTP/1.1 200 OK\\r\\nContent-Type: text/plain\\r\\n\\r\\nHello from C++ VPS!";\n        send(new_socket, response.c_str(), response.length(), 0);\n        close(new_socket);\n    }\n    return 0;\n}
\n\n

2.3. Building the Project

\n

Compile with optimization and static linking (if needed):

\n
g++ -O2 -std=c++17 main.cpp -o server\nls -la server  # Check the binary
\n\n

3. Deployment: systemd + Nginx reverse proxy

\n\n

3.1. Creating a systemd Service

\n

To ensure the application starts automatically on server boot and doesn't crash, create a systemd unit:

\n
nano /etc/systemd/system/cpp-server.se                

Blogs

Book Recommendations