{"id":2555,"date":"2020-02-15T11:37:19","date_gmt":"2020-02-15T08:37:19","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2555"},"modified":"2024-12-16T22:32:29","modified_gmt":"2024-12-16T19:32:29","slug":"cpp-fcgi","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2020\/02\/15\/cpp-fcgi\/","title":{"rendered":"Writing a backend server in C++ FCGI"},"content":{"rendered":"<p>A short note on how I wrote the server part for the 3D editor Cube Art Project, the server should save and display the work of users of the web version, giving them short URLs by the save button. At first I wanted to use Swift\/PHP\/Ruby\/JS or some similar modern language for the backend, but after looking at the characteristics of my VPS, it was decided to write the server in C\/C++.<br \/>First you need to install libfcgi on the server and the fcgi support module for your web server, example for Ubuntu and Apache:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>sudo apt install libfcgi libapache2-mod-fcgid\n\n<\/code><\/pre>\n<\/div>\n<p>Next, we configure the module in the config:<br \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2556\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/02\/module.png\" alt=\"\" width=\"545\" height=\"119\" srcset=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/02\/module.png 545w, https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/02\/module-300x66.png 300w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><br \/>\nFcgidMaxProcessesPerClass &#8211; maximum number of processes per class, I set it to 1 process because I don&#8217;t expect a heavy load.<br \/>AddHandler fcgid-script .fcgi &#8211; the extension of the file with which the fcgi module should start.<br \/>Add to the config the folder from which cgi applications will be launched:<br \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2557\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/02\/execgi.png\" alt=\"\" width=\"545\" height=\"82\" srcset=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/02\/execgi.png 545w, https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/02\/execgi-300x45.png 300w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><br \/>\nNext, we write an application in C\/C++ with fcgi support, compile it, and copy it to the \/var\/www\/html\/cgi-bin folder.<br \/>Examples of code and build script:<br \/><a href=\"https:\/\/gitlab.com\/demensdeum\/cube-art-project-server\/-\/blob\/master\/src\/cubeArtProjectServer.cpp\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/demensdeum\/cube-art-project-server\/-\/blob\/master\/src\/cubeArtProjectServer.cpp<\/a><br \/>\n<a href=\"https:\/\/gitlab.com\/demensdeum\/cube-art-project-server\/-\/blob\/master\/src\/build.sh\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/demensdeum\/cube-art-project-server\/-\/blob\/master\/src\/build.sh<\/a><br \/>\nAfter this you will need to restart your web server:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>systemctl restart apache2\n\n<\/code><\/pre>\n<\/div>\n<p>Next, set the necessary rights to execute the cgi-bin folder via chmod.<br \/>After that, your cgi program should work through the browser via the link, an example for the Cube Art Project server:<br \/><a href=\"http:\/\/192.243.103.70\/cgi-bin\/cubeArtProject\/cubeArtProjectServer.fcgi\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/192.243.103.70\/cgi-bin\/cubeArtProject\/cubeArtProjectServer.fcgi<\/a><br \/>\nIf something doesn&#8217;t work, then look at the web server logs, or connect to the running process with a debugger, the debugging process should not differ from the debugging process of a regular client application.<\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/154187\/\" target=\"_blank\" rel=\"noopener\">https:\/\/habr.com\/ru\/post\/154187\/<\/ a><br \/><a href=\"http:\/\/chriswu.me\/blog\/writing-hello-world-in-fcgi-with-c-plus-plus\/\" target=\"_blank\" rel=\"noopener\">http:\/\/chriswu.me\/blog\/writing-hello-world-in-fcgi-with-c-plus-plus\/<\/a><\/p>\n<h3>Source code<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/cube-art-project-server\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/cube-art -project-server<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A short note on how I wrote the server part for the 3D editor Cube Art Project, the server should save and display the work of users of the web version, giving them short URLs by the save button. At first I wanted to use Swift\/PHP\/Ruby\/JS or some similar modern language for the backend, but<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2020\/02\/15\/cpp-fcgi\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Writing a backend server in C++ FCGI&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[149],"class_list":["post-2555","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-fcgi-apache-c-c","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=2555"}],"version-history":[{"count":17,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2555\/revisions"}],"predecessor-version":[{"id":3921,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2555\/revisions\/3921"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=2555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=2555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=2555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}