How to Enable C++ in Apache on Gentoo
Without using framework! CGI for the Web is the foundation of how everything works in real life.
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!
Enabling Apache to interpret binaries created by C++
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:
01. Uncomment the CGI line
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.:
02. Enable CGI support for Apache
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!
03. Restart the Apache server
Use your bootloader to restart Apache. In my case I use OpenRC:
If you use SystemD use the corresponding command to restart Apache!
04. Change permissions and create the default directory for C++ codes
First of all, it is interesting to allow creating and reading CGI files, to do this assign the following permissions:
Now create the
Remembering that Gentoo separates
localhost/htdocs, that is, the public files are in
htdocsand the backend files are in
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
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
05. Testing C++ on the Web
Now let’s create a file inside the
And use the following example code:
Pay attention to the two
Content-Type, they are essential to avoid the Apache code interpretation error:
Internal Server Error.
If you have enabled
.cpptoo, you can save it as
index.cppor even without an extension!
If you want Apache to interpret
index.cpp without having to indicate the file in the URL, edit the file:
Look for the line that has the content:
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!
Watch a video with C++, Vue.js 3 and TailwindCSS running on Apache
The video is in Portuguese, but you can follow the code regardless of your language!
For more information visit the links: