HowTo : NginX - The Apache Replacement

How useful is this script/tutorial?


  • Total voters
    59

tsj5j

Verified User
Joined
Jun 22, 2007
Messages
24
Introduction / Requirements

Before reading this, please remember that NginX and Apache are fundamentally different.
Therefore, not all functions can be emulated.

When converting, be prepared to lose :
- .htaccess
I didn't do this, but I believe it should be doable for anyone who has too much time.
To me, they are really useless as accessing a script in /docroot/1/2/3/4/5/6 will force the server to do 7 or 8 stat calls all the way upwards.
Nothing near efficient if you ask me.

- mod_rewrite
This should be more doable than .htaccess in terms of implementing, as NginX has a powerful rewrite module in place.
However, I don't like extra stuff done per access, so this isn't useful for me.

In turn, you will gain :
- The ability to handle sync-floods and thousands of concurrent connections
- Use FastCGI for each user, creating a secure environment.
- Set a limit of FastCGI processes per user.
- Use php_admin_value and php_admin_flag with FastCGI
- Drastic Speed Improvements!

==========================================================

How To Install

Part 1 : NginX

1. Visit their website at http://www.nginx.net/
2. On their homepage, download either the stable or developmental version.

*Note: I went for the developmental version, but it doesn't really matter.*
*The command below is valid as of time of posting, but will probably be outdated.

3. Execute :

Code:
wget http://sysoev.ru/nginx/nginx-0.6.5.tar.gz
tar -xzf nginx-0.6.5.tar.gz
cd nginx-0.6.5

4. Configure according to your preference. I chose this :

Code:
./configure --with-http_stub_status_module --with-openssl=/usr/lib/openssl
make
make install

This ends the nginx install.

Part 2 : PHP

1. Prepare your configuration options. If PHP is already installed, create a phpinfo(); page and extract the configuration line.

2. Download the latest version of PHP, extract and enter that directory.

3. Execute

Code:
./configure --enable-fastcgi ... (Insert your configuration)
make
make install

This ends the PHP install.

Part 3 : Spawn FCGI Script

We need to download LightTPD to get it's spawn-fcgi.

1. To get it, download it from http://www.lighttpd.net.

2. Download, extract, and then run :

Code:
./configure
make
cd src
cp ./spawn-fcgi /usr/local/bin/spawn-fcgi

3. Try running /usr/local/bin/spawn-fcgi. Make sure it exists and returns this :

Code:
spawn-fcgi-1.4.15 - spawns fastcgi processes

4. You can remove the extracted folder. lighttpd has not been installed as make install was not executed.

Part 4 : Apache Parser

I've written a parser to parse Apache configuration (attached to this post).

1. Download the attachment, rename it nginx.php and place it in /etc/init.d/

2. Execute :

Code:
chmod 700 nginx.php

3. Use your favourite editor nano/pico/vi to open nginx.php.

4. At the top of the file, you should see :

Code:
#!/usr/local/bin/php -n

Replace /usr/local/bin/php with wherever your PHP binary is.
Be careful NOT to remove the ending -n.

5. The top part of the file (after that first line) should look like :

Code:
// Configuration Section
$nginx_init = "/etc/init.d/nginx";
$apache_config = "/etc/httpd/conf/httpd.conf";
$da_vhosts = "/etc/httpd/conf/extra/directadmin-vhosts.conf";
$tmp_folder = "/tmp/";
$debug_level = 0; // 0 => FATAL, 1 => WARN, 2 => INFO

$killall_bin = "/usr/bin/killall";
$spawn_bin = "/usr/local/bin/spawn-fcgi";

// Nginx Configuration
$nginx_config = "/usr/local/nginx/conf/nginx.conf";
$nginx_bin = "/usr/local/nginx/sbin/nginx";
$mod_status = true;

// PHP Configuration
$php_childs = 3;
$php_bin = "/opt/php/bin/php-cgi";
$php_startport = 11000;

Make sure the file paths are correct. For example, my PHP is located in /opt/php.
Yours probably isn't.

Some particularly important variables :

$nginx_init - Where the controllable nginx init script will be. Leaving as-is is probably fine.
$da_vhosts - This file must contain the list of includes of every user's httpd.conf
$tmp_folder - The temporary folder. Must be world writable.
$php_startport - The PHP FastCGI will bind to a port starting from this number.

6. Execute the php script was rewrite-init as the parameter.

Eg.
Code:
/etc/init.d/nginx.php rewrite-init

7. The init file will be written to wherever you specified it previously.

8. You can now run [init_file] start/stop/restart/reload/graceful/status.

Part 5 : DirectAdmin Touchup

1. Open /usr/local/directadmin/data/admin/services.status in a text editor.

2. Replace httpd=ON with nginx=ON.

3. Execute : (remember to replace INIT_FILE below with where your nginx init file is)

Code:
mv /etc/init.d/httpd /etc/init.d/httpd.old
ln -vs INIT_FILE /etc/init.d/httpd

Part 6 : htscanner (Optional)
This module installs a PHP plugin to emulate Apache's scanning for PHP options.

1. Locate where your PHP Pecl is :

Code:
locate pecl

2. Using the full path of pecl found, execute :

Code:
/location/to/pecl install htscanner

Installation has completed from this point onwards.
Please reply to this post if you find any bugs.


ChangeLog
v0.90 - FastCGI accepts php_admin_value, etc. configuration and uses its own user.
v1.00 - First Public Release.
v1.01 - Introduced new tutorial - htscanner
 

Attachments

  • nginx.txt
    14.5 KB · Views: 3,622
Last edited:

nobaloney

NoBaloney Internet Svcs - In Memoriam †
Joined
Jun 16, 2003
Messages
26,118
Location
California
No reason to post exactly the same twice. I deleted the other one.

Jeff
 

jackc

Verified User
Joined
Jan 19, 2007
Messages
347
very nice!
i want to make nginx handle static content on front of apache.
will try to modify a bit.
thanks
 

tsj5j

Verified User
Joined
Jun 22, 2007
Messages
24
very nice!
i want to make nginx handle static content on front of apache.
will try to modify a bit.
thanks

You'll probably want to change the binding port as well as the not remove apache's own init script.
 

jackc

Verified User
Joined
Jan 19, 2007
Messages
347
hi, I want to ask, is the bandwidth usage able to calculate? NginX seems have different log format.
 

tsj5j

Verified User
Joined
Jun 22, 2007
Messages
24
hi, I want to ask, is the bandwidth usage able to calculate? NginX seems have different log format.

DirectAdmin uses simply the byte format log of Apache to do calculation, and nginx supports custom formats, so I simply created a format in the config file to output only bytes.

Nginx also has another Apache-style full log which webalizer uses. It outputs that as well.
 

SoCal

Verified User
Joined
May 13, 2007
Messages
24
Hi,

I tried to follow this how-to, and not succeed.



./configure --with-http_stub_status_module --with-openssl=/usr/lib/openssl

It shows an error and aks to either add a no httpd rewrite module clause and one more option, its not in how-to what do do with this error.

Part 2 : PHP

1. Prepare your configuration options. If PHP is already installed, create a phpinfo(); page and extract the configuration line.

I am installing on a DA server, So what I am supposed to do?

Part 3 : Spawn FCGI Script

We need to download LightTPD to get it's spawn-fcgi.

1. To get it, download it from http://www.lighttpd.net.

I got the download of fast-cgi from fastcgi.com and when I installed by make command, You have specified to switch directory to src, but there is no directory inside named src.

Please explain how to perform all steps in as a detailed How-to for a noob.

Will this install make exactly same thing and normal working as it was working in apache?

Thanks for the info and initiative.
 

nick-a

Verified User
Joined
Feb 23, 2007
Messages
49
There seems to be a bug in the parser, I've got a vhost entry in an apache httpd.conf, but it's got loads of domain pointers in the serveralias, so has about 4-5 server alias entries. The parser only manages about 5 pointers. I tried adding them manually, but whenever I start nginx using the script, it overwrites my conf file.
 

smtalk

Administrator
Staff member
Joined
Aug 22, 2006
Messages
10,574
Location
LT, EU
conf/mime.types in nginx.php should be just mime.types, because conf/mime.types full path is /usr/local/nginx/conf/conf/mime.types, the same problem with conf/fastcgi.conf, it should be replaced to fastcgi_params. And for debian systems you should use killall instead of killproc in /etc/init.d/nginx. These lines are not needed too:
Code:
# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network
 
Last edited:

Yolte

Verified User
Joined
Oct 10, 2006
Messages
36
SSL implementation

Hello,

Is there any way to impelement apache openssl to nginx ?

when we restart nginx it gets all confs form

/etc/httpd/conf/extra/directadmin-vhosts.conf.

But it is not includes ;

SSLEngine on
SSLCertificateFile /usr/local/directadmin/data/users/user_name/domains/domain.com.cert
SSLCertificateKeyFile /usr/local/directadmin/data/users/user_name/domains/domain.com.key
SSLCACertificateFile /usr/local/directadmin/data/users/user_name/domains/domain.com.cacert

How can we convert SSL lines form httpd.conf to nginx.conf wiht nginx ssl format like this?
server {
listen 443;

ssl on;
ssl_certificate /etc/ssl/certs/myssl.crt;
ssl_certificate_key /etc/ssl/private/myssl.key;
 
Last edited:

dmtinc

Verified User
Joined
May 10, 2008
Messages
121
Location
Chile
hi, i have follow the how-to step by step, but i can execute php on my server, i get this

Code:
502 Bad Gateway
nginx/0.6.5

just its a phpinfo php file, any suggestion or help with this???

and when i try to enter to phpmyadmin www.domain.tld/phpmyadmin i get

Code:
404 Not Found
nginx/0.6.5


Thanks for your reply and help
Regards
Dmtinc
 
Last edited:

dmtinc

Verified User
Joined
May 10, 2008
Messages
121
Location
Chile
What is the output of /usr/local/bin/spawn-fcgi?

Code:
/usr/local/bin/spawn-fcgi
Usage: spawn-fcgi [options] -- <fcgiapp> [fcgi app arguments]

spawn-fcgi v1.4.19 - spawns fastcgi processes

Options:
 -f <fcgiapp> filename of the fcgi-application
 -a <addr>    bind to ip address
 -p <port>    bind to tcp-port
 -s <path>    bind to unix-domain socket
 -C <childs>  (PHP only) numbers of childs to spawn (default 5)
 -P <path>    name of PID-file for spawed process
 -n           no fork (for daemontools)
 -v           show version
 -h           show this help
(root only)
 -c <dir>     chroot to directory
 -u <user>    change to user-id
 -g <group>   change to group-id

Thanks For Your Help

Regards v,v
 

carter

New member
Joined
Jul 28, 2008
Messages
1
very nice!
i want to get a example of nginx configuration for PHP fastCGI's php_admin_value
i don't know how to configure the php_admin_value of php in nginx's configuration

Look forward the reply
thanks
 

proHSP

Verified User
Joined
Dec 22, 2007
Messages
161
Location
Moscow
Unfortunately, it is impossible, because nginx uses php as fastcgi, and fastcgi does not support php_admin_value.
 

nightskiff

New member
Joined
Nov 10, 2008
Messages
1
I've got the following error executing /etc/init.d/nginx.php rewrite-init


PHP Warning: PHP Startup: dbase: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: ldap: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: mbstring: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: mysql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: mysqli: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: odbc: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: PDO: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pdo_mysql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: PDO_ODBC: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pdo_pgsql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pdo_sqlite: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pgsql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
Rewrite completed.
[[email protected] init.d]# php -f /etc/init.d/nginx.php rewrite-init
PHP Warning: PHP Startup: dbase: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: ldap: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: mbstring: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: mysql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: mysqli: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: odbc: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: PDO: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pdo_mysql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: PDO_ODBC: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pdo_pgsql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pdo_sqlite: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
PHP Warning: PHP Startup: pgsql: Unable to initialize module
Module compiled with module API=20050922, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
Rewrite completed.
 
Last edited:
Top