How to Enable Apache Web Server for C++ on Windows
Simple and easy!
In this article we will see how to install Apache from scratch and prepare your environment on Windows for development web with Apache and C++.
Let’s go step by step!
01. Install Apache
1. Visit: https://www.apachelounge.com/.
2. Click Downloads on the left corner
Download the file: Apache 2.4.57 Win64 | [Apache VS17 Win64 Binary] httpd-2.4.57-win64-VS17.zip
3. Unzip the file: Extract to httpd-2.4.57-win64-VS17
4. Move the unzipped folder to the root of the :C\
drive
5. Open CMD (Run as Administrator) and enter the folder: cd C:\Apache24\bin\
6. Run the command: httpd -k install
7. Now enter the folder: C:\Apache24\conf and open the file: httpd.conf
with Notepad
8. I renamed the line that has:
#ServerName www.example.com:80 to ServerName localhost (remember to remove the junk (#) from the beginning of the line), save!
FROM:
TO:
9. Now in CMD, restart Apache by running the command: httpd -k stop
and then httpd -k start
10. Now open the browser and access: http://localhost/
If the phrase: It works! appears, everything is ok, if not, repeat all the steps, because this procedure should work!
02. Run C++ on the Web
1. Enter the folder: cd C:\Apache24\cgi-bin
and create a file named main.cpp
and insert this content inside:
#include <iostream>
int main(int argc, char **argv){
std::cout << "Content-Type: text/html\n\n";
std::cout << "<h1>C++ running on the Web on Windows via Apache</h1>" << '\n';
return 0;
}
2. Compile the code (it’s a good idea to use MinGW ) and save the binary ( can be named index.html
) within the same folder:
g++ main.cpp -o index.html
3. Access the address: http://localhost/cgi-bin/, it is important to put the slash (/
) at the end.
4. If you want Apache to automatically render index.cpp
, edit the file: C:\Apache24\conf\httpd.conf
, look for the line: DirectoryIndex index.html
and add index. cpp
at the end, looking like this:
<IfModule dir_module>
DirectoryIndex index.html index.cpp
</IfModule>
Now just compile to binary: index.cpp
(remove index.html
) RESTART Apache and access: http://localhost/cgi-bin/.
See the video
The video below is in Portuguese, but you can watch the procedure if you have any difficulties.
Comments