Installation and configuration details of Nginx in Ubuntu _linux

Source: Internet
Author: User
Tags install php php and mysql

1.Nginx Introduction

Nginx is a very lightweight HTTP server, Nginx, which 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

Currently, there are three kinds of Web server support for PHP:

(1) Through the Web server built-in modules to implement, such as Apache MOD_PHP5, similar Apache built-in Mod_perl

can be supported for Perl.

(2) implemented through CGI, this is like the Perl CGI, which has the disadvantage of poor performance, because each server encounters

All of these scripts need to restart the script parser to execute the script and then return the results to the server;

On the other hand, it's less secure; it's almost rarely used.

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

Initiates one or more daemon processes, which are delivered directly to the fastcgi process each time the Web server encounters a script, and then

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

2.1 apache+mod_php Mode

We used classic apache+mod_php for a long time.

Apache Support for PHP is supported by the Apache module. If you want to install PHP from source code, if you expect Apache support

PHP, in the./configure step needs 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 PHP5, and in the final step make install we see the dynamic link library

Libphp5.so is copied to the modules directory of the APACHE2 installation directory, and you also need to add loadmodule to the httpd.conf configuration file

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

2.2 nginx+fastcgi Mode

Nginx is completely lightweight, you have to use a Third-party fastcgi processor to parse PHP, so in fact it seems that Nginx is

Very flexible, it can be connected with any third party to provide a resolved processor to enable parsing of PHP (easily set in nginx.conf).

Nginx can use spwan-fcgi. Installation of LIGHTTPD is required in earlier versions, but spawn-fcgi can be installed directly after version 9.10.

Now there's a new third-party PHP fastcgi processor called PHP-FPM that you can learn about. 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, it originally belonged to lighttpd this bag inside, but after 9.10 spawn-fcgi

were separated out into bundles alone.

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

$sudo Apt-get Install spawn-fcgi

(2) The source code installation is as follows, the download address is:

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

After decompression, enter the directory to perform the following installation command:

$./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.

After Ubuntu installs Nginx, the file structure is roughly:

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

Startup program Files in/usr/sbin/nginx

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

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

The default virtual host's directory setting is in the/usr/share/nginx/www

(2) Source code installation

Download Address: 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 for the Conf directory under the NGINX.CONF,

Nginx boot files in the Sbin directory nginx files.

3.2 Start Nginx

(1) The starting process of online installation

$sudo/etc/init.d/nginx Start

(2) The startup process of the source code installation

$CD/usr/local/nginx

$sbin/nginx

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

Resolve and then continue.

If your machine has Apache installed at the same time, the access is not available, and Nginx may not start, which is

Because they all use 80 of this port. Here we modify the port of Nginx to 8080,

This is mainly to modify the Nginx configuration file nginx.conf, this line

Listen 80;

Amended to

Listen 8080;

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

3.3 Installing PHP and MySQL

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

3.4 Test Nginx support for PHP

(1) Reboot 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 When an error occurs, check to see if php-cgi is installed, if not, 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, where some of the necessary parameters are set, and we find such statements as:

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 nginx of the source code installation, the configuration file is/usr/local/nginx/conf/nginx.conf.

The following describes the matching rules for location:

(1) = prefix of the instructions to strictly match this query. If found, stop the search.

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

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

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

General strings and regular expressions can be used in location.

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

(1) ~* prefix selection case-insensitive matching

(2) ~ Select Match Case Matching

Example:

Location =/{

# only match/query.

[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 searching.

# any regular expression will not be tested.

[Configuration C]

}

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

# matches any requests that end in GIF, JPG, or JPEG.

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

[Configuration D]

}

Here you also have a certain understanding of regular expressions!!!

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.