Set the Hostname
Before you begin installing and configuring the components described in this guide, please make sure you’ve followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:
hostname hostname -f |
The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).
Install Required Packages
CentOS doesn’t include nginx in their repositories, so you’ll need to add support for EPEL (Extra Packages for Enterprise Linux) from the Fedora project. Issue the following command:
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm |
Issue the following commands to update your system and install the nginx web server and compiler tools (Perl should already be installed):
yum update yum install nginx make automake gcc gcc-c++ fcgi-perl wget yum install nginx fcg-perl wget chkconfig --add nginx chkconfig nginx on /etc/init.d/nginx start |
You’ll be asked to accept the key for EPEL, as it gets imported the first time you install an EPEL package.
Configure Virtual Hosting
In this guide, the domain “example.com” is used as an example site. You should substitute your own domain name in the configuration steps that follow. First, create directories to hold content and log files:
mkdir -p /srv/www/www.example.com/public_html mkdir /srv/www/www.example.com/logs chown -R nginx:nginx /srv/www/www.example.com |
Issue the following commands to create virtual hosting directories:
mkdir /etc/nginx/sites-available mkdir /etc/nginx/sites-enabled |
Add the following lines to your /etc/nginx/nginx.conf
file, immediately after the line for include /etc/nginx/conf.d/*.conf
:
- /etc/nginx/nginx.conf
-
# Load virtual host configuration files. include /etc/nginx/sites-enabled/*;
Next, you’ll need to define the site’s virtual host file:
- /etc/nginx/sites-available/www.example.com
-
server { listen 80; server_name www.example.com example.com; access_log /srv/www/www.example.com/logs/access.log; error_log /srv/www/www.example.com/logs/error.log; location / { root /srv/www/www.example.com/public_html; index index.html index.htm; } location ~ .pl$ { gzip off; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:8999; fastcgi_index index.pl; fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name; } }
Issue the following commands to enable the site:
cd /etc/nginx/sites-enabled/ ln -s /etc/nginx/sites-available/www.example.com /etc/init.d/nginx restart |
You may wish to create a test HTML page under /srv/www/www.example.com/public_html/
and view it in your browser to verify that nginx is properly serving your site (Perl will not work yet). Please note that this will require an entry in DNS pointing your domain name to your Linode’s IP address (found on the “Remote Access” tab in the Linode Manager).
Configure FastCGI Wrapper
First create the FastCGI wrapper script (credit: Denis S. Filimonov) at /usr/bin/fastcgi-wrapper.pl
with the following contents:
- /usr/bin/fastcgi-wrapper.pl
-
#!/usr/bin/perl use FCGI; use Socket; use POSIX qw(setsid); require 'syscall.ph'; &daemonize; #this keeps the program alive or something after exec'ing perl scripts END() { } BEGIN() { } *CORE::GLOBAL::exit = sub { die "fakeexitnrc=".shift()."n"; }; eval q{exit}; if ($@) { exit unless $@ =~ /^fakeexit/; }; &main; sub daemonize() { chdir '/' or die "Can't chdir to /: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; umask 0; } sub main { $socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 ); #use IP sockets $request = FCGI::Request( *STDIN, *STDOUT, *STDERR, %req_params, $socket ); if ($request) { request_loop()}; FCGI::CloseSocket( $socket ); } sub request_loop { while( $request->Accept() >= 0 ) { #processing any STDIN input from WebServer (for CGI-POST actions) $stdin_passthrough =''; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){ my $bytes_read = 0; while ($bytes_read < $req_len) { my $data = ''; my $bytes = read(STDIN, $data, ($req_len - $bytes_read)); last if ($bytes == 0 || !defined($bytes)); $stdin_passthrough .= $data; $bytes_read += $bytes; } } #running the cgi app if ( (-x $req_params{SCRIPT_FILENAME}) && #can I execute this? (-s $req_params{SCRIPT_FILENAME}) && #Is this file empty? (-r $req_params{SCRIPT_FILENAME}) #can I read this file? ){ pipe(CHILD_RD, PARENT_WR); my $pid = open(KID_TO_READ, "-|"); unless(defined($pid)) { print("Content-type: text/plainrnrn"); print "Error: CGI app returned no output - "; print "Executing $req_params{SCRIPT_FILENAME} failed !n"; next; } if ($pid > 0) { close(CHILD_RD); print PARENT_WR $stdin_passthrough; close(PARENT_WR); while(my $s = <KID_TO_READ>) { print $s; } close KID_TO_READ; waitpid($pid, 0); } else { foreach $key ( keys %req_params){ $ENV{$key} = $req_params{$key}; } # cd to the script's local directory if ($req_params{SCRIPT_FILENAME} =~ /^(.*)/[^/]+$/) { chdir $1; } close(PARENT_WR); close(STDIN); #fcntl(CHILD_RD, F_DUPFD, 0); syscall(&SYS_dup2, fileno(CHILD_RD), 0); #open(STDIN, "<&CHILD_RD"); exec($req_params{SCRIPT_FILENAME}); die("exec failed"); } } else { print("Content-type: text/plainrnrn"); print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not "; print "exist or is not executable by this process.n"; } } }
Then create an init script to control the FastCGI process that matches the one shown below:
- /etc/rc.d/init.d/perl-fastcgi
-
#!/bin/sh # # nginx – this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /opt/nginx/conf/nginx.conf # pidfile: /opt/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 perlfastcgi="/usr/bin/fastcgi-wrapper.pl" prog=$(basename perl) lockfile=/var/lock/subsys/perl-fastcgi start() { [ -x $perlfastcgi ] || exit 5 echo -n $"Starting $prog: " daemon $perlfastcgi retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { echo -n $”Reloading $prog: ” killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac
Next issue the following commands to make the scripts executable and set the perl-fastcgi process to start on boot:
chmod +x /usr/bin/fastcgi-wrapper.pl chmod +x /etc/rc.d/init.d/perl-fastcgi /etc/rc.d/init.d/perl-fastcgi start chkconfig --add perl-fastcgi chkconfig perl-fastcgi on |
Test Perl with FastCGI
Create a file called “test.pl” in your site’s “public_html” directory with the following contents:
- /srv/www/www.example.com/public_html/test.pl
-
#!/usr/bin/perl print "Content-type:text/htmlnn"; print <<EndOfHTML; <html><head><title>Perl Environment Variables</title></head> <body> <h1>Perl Environment Variables</h1> EndOfHTML foreach $key (sort(keys %ENV)) { print "$key = $ENV{$key}<br>n"; } print "</body></html>";
Make the script executable by issuing the following command:
1 |
chmod a+x /srv/www/www.example.com/public_html/test.pl |
When you visit http://www.example.com/test.pl
in your browser, your Perl environment variables should be shown. Congratulations, you’ve configured the nginx web server to use Perl with FastCGI for dynamic content!
# wget http://ftp.mozilla.org/pub/mozilla.org/webtools/bugzilla-4.4.tar.gz
Unpack it to the desired directory:
# tar zxvf bugzilla-4.2.2.tar.gz -C /var/www/html/
Enter the directory that contains the unpacked Bugzilla installation and rename the “bugzilla-${VERSION}” directory to something more convenient for web access:
# cd /var/www/html/ # mv -v bugzilla-4.4 bugzilla
Login to mysql using ‘root’ as user and your mysql ‘root’ password, and create a database for the Bugzilla installation:
# mysql -uroot -p
mysql> create database bugzilla_DB;
Grant all privileges on the database you just created to a newly created user and identify it by his own password:
mysql> grant all on bugzilla_DB.* to some_user@localhost identified by 'random_password';
and exit mysql:
mysql> q
Run the following command to find the necessary modules:
# ./checksetup.pl
After the checksetup.pl script is done, install the necessary and the optional modules by executing:
# /usr/bin/perl install-module.pl --all
Once the modules are installed, run the checksetup.pl script again to generate the localconfig file:
# ./checksetup.pl
Use your favorite text editor (in this case vim) and edit the $db_name, $db_user and $db_pass entries in the ‘localconfig’ file and fill them with the mysql database information we set earlier:
# vim ./localconfig
Again, run the checksetup script to set initial configuration:
# ./checksetup.pl
Next, these few lines need to be appended to the apache config file:
# vim /etc/httpd/conf/httpd.conf
<Directory /var/www/html/bugzilla>
AddHandler cgi-script .cgi
Options +Indexes +ExecCGI
DirectoryIndex index.cgi
AllowOverride Limit FileInfo Indexes
< /Directory>
Save the file and execute the following command to restart the webserver:
# service httpd restart
That’s it! Open http://<IP_ADDRESS_OR_HOSTNAME>/bugzilla and start organizing your software development!
NOTE: In case of “500 Internal Server Error” on your first try to access Bugzilla, please check for the “Options” line in the .htaccess file and comment it out.
Of course, if you are one of our Linux VPS Hosting customers, you don’t have to do any of this, simply ask our admins, sit back and relax. Our admins will install Bugzilla for you immediately.
PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
- The nginx Homepage
- FastCGI Project Homepage
- Perl Documentation
- Installing Nginx on CentOS 5
- Basic Ngnix Configuration