Compile a simple cgi script in C in Ubuntu + Apache2 Environment

Source: Internet
Author: User
I have only studied c language, but I have never studied prel. Many tutorials on the Internet are for prel, and few are for c. I spent an afternoon in Ubuntu, and finally wrote a helloworld cgi in c. This is the first step to get started with cgi. 1. install and configure apache server install apache2 server # sudoapt-getinstallapache2 configuration apache2 server configuration file in/etc/apache2/site

I have only studied c language, but I have never studied prel. Many tutorials on the Internet are for prel, and few are for c. I spent an afternoon in Ubuntu, and finally wrote a helloworld cgi in c. This is the first step to get started with cgi.

1. install and configure the apache server

Install the apache2 server
# Sudo apt-get install apache2

Configure the apache2 server
The configuration file is located at/etc/apache2/sites-enabled/000-default.
Open the configuration file with vi:
# Sudo vi/etc/apache2/sites-enabled/000-default
Modify the following two sentences:
DocumentRoot/var/www/html
ScriptAlias/cgi-bin // var/www/html/cgi-bin/
Pay attention to the space issue.

You can set these two directories by yourself, and the Set directories must actually exist. If they do not exist, mkdir will generate one.
DocumentRoot is followed by a directory containing HTML files.
ScriptAlias is followed by/cgi-bin/to/var/www/html/cgi-bin/, that is, the files in this directory are considered as cgi programs.
For example, if you access http: // 127.0.0.1/cgi-bin/hello. cgi in a browser, You can execute hello. cgi in cgi-bin.
Save the configuration.

Restart apache2:
# Sudo/etc/init. d/apache2 restart


2. Compile the hello. cgi program

Create a hello. c file under cgi-bin:
# Sudo vi hello. c
Write the following content:
# Include

Int main ()

{
Printf ("Content-Type: text/html \ n ");

Printf ("Hello, world \ n ");

Return 0;
}
Save and exit.

Compile:
# Sudo gcc hello. c-o hello. cgi
Run the command first.
# Sudo./hello. cgi
Check whether helloworld is output.

Then, you can type http: // 127.0.0.1/cgi-bin/hello. cgi in the browser.
The browser can display helloworld.

Iii. Permission issues

My method is brutal. I set the permissions of all related files and directories to 777.
# Sudo chmod 777 [file name or directory]
Here, we should first enter the door, and we will have time to study the permissions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.