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.


How to Enable C++ in Apache on Gentoo


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:

emerge apache

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:

sudo vim /etc/apache2/modules.d/00_mod_mime.conf

And uncomment the line: AddHandler cgi-script. It will be like this:

#AddHandler cgi-script .cgi

Leave it like this:

AddHandler cgi-script .cgi

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:

AddHandler cgi-script .cgi .cpp

You can even add more extensions there, if you are interested, e.g.: .cc, .c, .hpp, .h, …


02. Enable CGI support for Apache

Open the file:

sudo vim /etc/conf.d/apache2

Look for the line that has the variable APACHE2_OPTS and it looks something like this:

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST"

Add support for CGI by inserting -D CGI at the end:

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D CGI"

If you want, also add -D CPP to the end, just like you do when installing PHP (-D PHP), leaving it like this:

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D CGI -D CPP"

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:

sudo rc-service apache2 restart

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:

sudo chown -R $USER:$USER /var/www/localhost

Now create the cgi-bin directory:

Remembering that Gentoo separates localhost/htdocs, that is, the public files are in htdocs and the backend files are in localhost/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:

cd /var/www/localhost
mkdir cgi-bin

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.


05. Testing C++ on the Web

Now let’s create a file inside the localhost/cgi-bin directory:

cd /var/www/localhost/cgi-bin
vim main.cpp

And use the following example code:

Pay attention to the two /n/n in Content-Type, they are essential to avoid the Apache code interpretation error: Internal Server Error.

#include <iostream>

int main(int argc, char **argv){
   std::cout << "Content-Type: text/html\n\n";
   std::cout << "<h1>C++ runs great on the Web!</h1>" << '\n';
   return 0;
}

Compile normally:

g++ main.cpp -o index.html

If you have enabled AddHandler for .cpp too, you can save it as index.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:

sudo vim /etc/apache2/modules.d/00_default_settings.conf

Look for the line that has the content:

<IfModule dir_module>
DirectoryIndex index.html index.html.var
</IfModule>

And add index.cpp right after index.html.var leaving it like this:

<IfModule dir_module>
DirectoryIndex index.html index.html.var index.cpp
</IfModule>

And restart the server:

sudo rc-service apache2 restart

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!

https://youtu.be/kTySQQT7_Sg


For more information visit the links:


web cpp apache Web development


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles