Introduction: Why Deploy a C++ Application on a VPS?
\nC++ 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.
\nIn 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\n1. VPS Preparation and Tool Installation
\n\n1.1. Connecting to the Server
\nAfter purchasing a VPS, you will receive an IP address, a login (usually root), and a password. Connect via SSH:
\nssh root@your_server_IP\nWe recommend immediately updating the package database and installing basic utilities:
\napt update && apt upgrade -y\napt install git curl wget build-essential -y\n\n1.2. Installing the Compiler and CMake
\nTo build a C++ project, you will need g++ and cmake:
apt install g++ cmake -y\ng++ --version # Check version\nIf you need a newer version of GCC (e.g., for C++20), add the PPA:
\nadd-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\n2. Writing a Test C++ Application (HTTP Server)
\nFor 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\n2.1. Project Structure
\nmkdir ~/cpp-server && cd ~/cpp-server\nnano main.cpp\n\n2.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\n2.3. Building the Project
\nCompile with optimization and static linking (if needed):
\ng++ -O2 -std=c++17 main.cpp -o server\nls -la server # Check the binary\n\n3. Deployment: systemd + Nginx reverse proxy
\n\n3.1. Creating a systemd Service
\nTo ensure the application starts automatically on server boot and doesn't crash, create a systemd unit:
\nnano /etc/systemd/system/cpp-server.se