CGI for the Web is the basis of how everything works in real life. You can enable support for C++ on any operating system.
In Gentoo it’s the same process but with different paths and addition tools, let’s see how to do it!
First of all. Of course, you need Apache installed:
Currently, by default, Apache on Gentoo already has support for CGI, so there is no need to add flags to make.conf
(~APACHE2_MODULES=”cgi”~). So, just edit the following files:
Another thing that also works as standard is the line LoadModule cgi_module modules/mod_cgi.so
in the file: /etc/apache2/httpd.conf
, that is, you don’t need to do anything, but it’s always a good idea to check to see if it really is uncommented.
However, open the file:
And uncomment the line: AddHandler cgi-script
. It will be like this:
Leave it like this:
If you want Apache to also run URLs with the .cpp
extension (by default it only reads .html
and for those who have PHP installed, it also reads .php
), add it to the end of the line, like this:
You can even add more extensions there, if you are interested, e.g.:
.cc
,.c
,.hpp
,.h
, …
Open the file:
Look for the line that has the variable APACHE2_OPTS
and it looks something like this:
Add support for CGI by inserting -D CGI
at the end:
If you want, also add -D CPP
to the end, just like you do when installing PHP (-D PHP
), leaving it like this:
In the case of PHP this is mandatory, but for C++ this part is optional!
Use your bootloader to restart Apache. In my case I use OpenRC:
If you use SystemD use the corresponding command to restart Apache!
First of all, it is interesting to allow creating and reading CGI files, to do this assign the following permissions:
Now create the cgi-bin
directory:
Remembering that Gentoo separates
localhost/htdocs
, that is, the public files are inhtdocs
and the backend files are inlocalhost/cgi-bin
.
Generally, Apache itself creates this directory, if it already exists, it is also recommended to change the owner of this directory so you can create and edit files without needing to use sudo
or root
, but if it doesn’t exist, create the directory:
If it already exists, don’t forget to become its owner:
sudo chown -R $USER:$USER /var/www/localhost/cgi-bin
.
On APT-based systems (Debian, Ubuntu, Mint,…) this directory is in /usr/lib/cgi-bin
.
Now let’s create a file inside the localhost/cgi-bin
directory:
And use the following example code:
Pay attention to the two
/n/n
inContent-Type
, they are essential to avoid the Apache code interpretation error:Internal Server Error
.
Compile normally:
If you have enabled
AddHandler
for.cpp
too, you can save it asindex.cpp
or even without an extension!
Now just access the understanding: http://localhost/cgi-bin/ or http://localhost/cgi-bin/index.cpp (if you enabled it and saved it as index.cpp
)
If you want Apache to interpret index.cpp
without having to indicate the file in the URL, edit the file: /etc/apache2/modules.d/00_default_settings.conf
:
Look for the line that has the content:
And add index.cpp
right after index.html.var
leaving it like this:
And restart the server:
It is possible to do absolutely everything: GET, POST, Authentication, Sessions, Database, Tokens, Cookies,… with C++ in Apache, in fact, many famous sites that you access using C++ on the Web!
The video is in Portuguese, but you can follow the code regardless of your language!
https://youtu.be/kTySQQT7_Sg
For more information visit the links:
web cpp apache Web development