Ubuntu Nginx Server Installation Configuration tutorial

Source: Internet
Author: User
Tags php and php and mysql regular expression nginx server

Nginx is a very lightweight HTTP server and a high-performance HTTP and reverse proxy server, as well as a IMAP/POP3/SMTP proxy server.

1. 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 support Perl.

(2) implemented by CGI, this is like the previous Perl CGI, which has the disadvantage of poor performance, because every time the server encounters these scripts, it needs 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;

(3) The latest appearance is called fastcgi. The so-called fastcgi is the improvement of CGI. It generally uses C/s structure, the general script processor will start one or more daemon processes, each time the Web server encountered the script, directly to the fastcgi process to execute, and then the resulting results (usually HTML) returned to the browser.

1.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 PHP is compiled from source code, if you want Apache to support PHP,

In the./configure step you need to specify the--WITH-APXS2=/USR/LOCAL/APACHE2/BIN/APXS representation to tell the compiler to provide via Apache MOD_PHP5/APXS

parsing of the PHP5; and in the final step of make install we see the modules directory where the libphp5.so copy of the dynamic link library to the Apache2 installation directory

, and you also need to add the LoadModule statement to the httpd.conf configuration file to dynamically load the Libphp5.so module in order to achieve Apache support for PHP.

1.2 nginx+fastcgi Mode

Nginx is completely lightweight, you have to use a Third-party fastcgi processor to parse PHP, so in fact it looks like Nginx is very flexible, it can

and any third party to provide a resolution of the processor implementation of the connection to achieve the PHP parsing (easily set in the 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's fastcgi processor, called PHP-FPM, can be understood. This article is based on spawn-fcgi implementation of the PHP module support.

1.3 Installation FastCGI

/usr/bin/spawn-fcgi this file to manage FastCGI, it belonged to lighttpd this package, but after 9.10, spawn-fcgi was separated into a separate package.

(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.

2.Nginx Installation

2.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

Under the Nginx file.

2.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

And 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 and then go ahead.

If your machine has Apache installed at the same time, the access is not available, and Nginx may not start, because they are all using the 80 port. Here we will modify the port of Nginx to 8080, which mainly modifies the Nginx configuration file nginx.conf, this line

Listen 80;

Amended to

Listen 8080;

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

23 Installing PHP and MySQL

$sudo apt-get Install Php5-cliphp5-cgimysql-server php5-mysql

2.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

3.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.