C++ web development environment setup on CentOS
Category: programming
Web programming essentially is about text processing–at least it was mainly about getting requests in the form of text and then response with text/html as well. Of course later some more file formats are supported and currently we even have just JSON based reponses–the main stream(like JSON, html, JavaScript and CSS) is still text based. For processing text, scripting languages like Perl, PHP, Python and Ruby are great and therefore we see popular web development technologies are mostly based on these languages(Now we have Java, Scala, Go, Erlang and many more choices as well though). So spending time figuring out how C++ is going to do this job just does not seem to be right.
What do we use C++ for? System and application programmings. Google used to have a lot of C++ code for its search engine because it is just so performance critical. Facebook grew to a huge scale and found that PHP is on longer serving them well because of slowness and typelessness. Then they have Hack(OCaml package for setting types and compiled to C++ for speed). But for most web development, we do not need C++ for performance enhancement. But by exploring C++ in web development, we can understand fundamentals of web development better–and that is the main purpose of this tutorial.
Before we start, you may want to first get a good look at HTTP and CGI
The tutorial is based on CentOS 7. If you do not have one, you can use VirtualBox to install it in a virtual machine and you can even explore use of Vagrant.
First, let us just check and install essential developments tools:
You may want to just configure firewall and SELinux settings here for simplicity so that you will not just get “Permission denied” message without any clue why.
There are basically 2 choices for setting up web server to serve CGI scripts. Apache and httpd support execution of CGI scripts by default and you can just simply enable “+ExecCGI” option. Or you can try Nginx, which does not support CGI execution but supports FastCGI. So in this tutorial we will be exploring the easier option and we will be install htttpd to server CGI scripts.
Install httpd with the command:
Navigate to httpd configuration folder and edit httpd.conf, and add the following lines
Restart httpd server with systemctl reload httpd
. Now navigate to /var/www/cgi-bin/ folder and create start.cpp like this:
Compile with command g++ -o start.cgi start.cpp
and make it executable with chmod +x start.cgi
. Then curl localhost/cgi-bin/start.cgi
to see the response.
Note: CGI scripts are any executable scripts. For example you can just create a Perl script or a Python script and print the html response you want to see
References: