Installation and configuration of Nginx in Ubuntu

Source: Internet
Author: User

1.Nginx Introduction

Nginx is a very lightweight HTTP server, Nginx, it is pronounced "engine X", is a high-performance http and

The reverse proxy server is also a IMAP/POP3/SMTP proxy server.

2. support for PHP

There are currently three types of Web server support for PHP:

(1) implemented via a Web server built-in module, such as Apache MOD_PHP5, similar to Apache built-in mod_perl

Perl support is available.

(2) by CGI, this is like before Perl CGI, the disadvantage of this way is poor performance, because each time the server encountered

These scripts need to restart the script parser to execute the script and return the results to the server;

On the other hand, it is not very safe;

(3) A new appearance called fastcgi. The so-called fastcgi is the improvement of CGI. It generally uses C/s structure, general script processor

One or more daemon processes are started, each time the Web server encounters a script, it is delivered directly to the fastcgi process to execute, and then

Returns the resulting result (usually HTML) to the browser.

2.1 apache+mod_php Mode

We used the classic apache+mod_php for a long time.

The Apache support for PHP is supported by the Apache module. If you are installing PHP from source code, if you want Apache to support

PHP words, in the./configure step you need to specify the --WITH-APXS2=/USR/LOCAL/APACHE2/BIN/APXS representation to tell the compiler to pass

Apache Mod_php5/apxs to provide parsing of the PHP5, and in the final step make install we will see the dynamic link library

The libphp5.so is copied to the modules directory of the APACHE2 installation directory, and you also need to add LoadModule in the httpd.conf configuration file

Statement to dynamically load the Libphp5.so module in order to enable Apache support for PHP.

2.2 nginx+fastcgi Mode

Nginx is completely lightweight, must rely on third-party fastcgi processor to be able to parse PHP, So in fact, it seems that Nginx is

Very flexible, it can be connected with any third party to provide parsing processor to realize the parsing of PHP (easy to set in nginx.conf).

Nginx can use spwan-fcgi. Installation of LIGHTTPD is required in earlier versions, but it is possible to install spawn-fcgi directly after version 9.10.

Now there is a new third-party PHP fastcgi processor, called PHP-FPM, that you can look at. This paper is based on spawn-fcgi implementation of

Support for PHP modules.

2.3 installation FastCGI

/usr/bin/spawn-fcgi this file to manage FastCGI, which originally belonged to lighttpd inside this package, but after 9.10, spawn-fcgi

are separated out into packs alone.

(1) Use the Apt-get Online installation command as follows:

$sudo Apt-get Install spawn-fcgi

(2) The source code is installed as follows:

Http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz

After decompression, proceed to the following installation command in the directory:

$./configure

$make

$make Install

After installation, the SPAWN-FCGI command can be used directly, and its executable file is/usr/local/bin/spawn-fcgi.

3.Nginx installation

3.1 installation Nginx

(1) Online installation

$sudo Apt-get Install Nginx

The Nginx version is 1.2.1

Ubuntu after installing Nginx file structure is roughly:

All configuration files are under/etc/nginx, and each virtual host is already under/etc/nginx/sites-available

Startup program Files in/usr/sbin/nginx

The logs were placed in the/var/log/nginx, Access.log and Error.log, respectively.

and has created a startup script Nginx under/etc/init.d/

The default virtual host directory is set to/usr/share/nginx/www

(2) Source code installation

: http://nginx.org/download/

I am here to download the nginx-1.3.9.tar.gz, the installation process is very simple, as follows:

$./configure

$make

$make Install

After successful installation, Nginx placed in the/usr/local/nginx directory, the main configuration file is the conf directory nginx.conf,

Nginx startup file in the Sbin directory of Nginx file.

3.2 Start Nginx

(1) Start-up process of online installation

$sudo/etc/init.d/nginx Start

(2) Source code installation start-up process

$CD/usr/local/nginx

$sbin/nginx

Then you can visit, http://localhost/, everything is OK! If you can't access it, don't go ahead and see what the reason is,

Resolve before continuing.

If your machine has Apache installed at the same time, the above access mode can not be used, and Nginx may not start, which is

Because they're all using 80 of this port. We will change the Nginx port to 8080,

Here is the main modification of nginx configuration file nginx.conf, this line

Listen 80;

Revision changed to

Listen 8080;

Then you can access it, http://localhost:8080/.

3.3 installation PHP and the MySQL

$sudo apt-get Install php5-cli php5-cgi mysql-server php5-mysql

3.4 Test Nginx the PHP the support

(1) Restart Nginx:

$/etc/init.d/nginx restart

(2) Start fastcgi:

$spawn-fcgi-a 127.0.0.1-p 9000-c 10-u www-data-f/usr/bin/php-cgi

spawn-fcgi start error, check if php-cgi is installed, if yes, install php5-cgi.

$sudo Apt-get Install php5-cgi

(3) test

Open http://localhost/phpinfo.php

4.Nginx Configuration

The Nginx configuration file is/etc/nginx/nginx.conf, which sets up some of the necessary parameters, and we find one of these statements:

include/etc/nginx/sites-enabled/*

You can see that the/etc/nginx/sites-enabled/default file is also a core configuration file that contains the main configuration information,

such as server and directory, server name, location information, and server information.

For the source code installation Nginx, the configuration file is/usr/local/nginx/conf/nginx.conf.

The following main description of location matching rules:

(1) = prefix instruction strictly matches this query. If found, stop the search.

(2) The remainder of the regular string, the longest match is used first. If this match uses the ^~ prefix, the search stops.

(3) Regular expressions, in the order of the configuration file, the first match is used.

(4) If the third step produces a match, the result is used. Otherwise, the matching result of the second step is used.

You can use regular strings and regular expressions in location.

If you use regular expressions, you must use the following rules:

(1) ~* prefix selection case-insensitive matching

(2) ~ Select a case-sensitive match

Example:

Location =/{

# Match/Query only.

[Configuration A]
}

Location/{

# matches any query, because all requests start with/.

# but regular expression rules and long block rules will be matched by precedence and query.

[Configuration B]

}

Location ^~/images/{

# matches any query that starts with/images/and stops the search.

#任何正则表达式将不会被测试.

[Configuration C]

}

Location ~*\. (Gif|jpg|jpeg) $ {

#匹配任何以 the end of a GIF, JPG, or JPEG request.

# However, all requests for/images/directories will use Configuration C.

[Configuration D]

}

Here you also have a certain understanding of the regular expression!!!

Installation and configuration of Nginx in Ubuntu

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.