Installation and configuration details of Nginx in Ubuntu

  • 2020-05-09 19:40:57
  • OfStack

1. Nginx is introduced

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

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

2. Support for PHP

Currently,         servers support PHP in 3 ways:

    (1) is implemented through built-in modules in web servers, such as mod_php5 in Apache, and mod_perl in Apache similarly

Support for perl is available.

    (2) is implemented by CGI, which is similar to CGI of perl before. The disadvantage of this method is poor performance, because every time the server encounters it

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

The other one is less safe; This aspect is rarely used anymore.

    (3) the latest one is called FastCGI. FastCGI is an improvement on CGI. It USES an C/S structure and a script processor

One or more daemon processes will be started, and each time the web server encounters a script, it will be delivered directly to the FastCGI process for execution, and then

The result (usually html) is returned to the browser.

2.1 Apache + mod_php mode

        we used the classic Apache+mod_php for a long time.

        Apache's support for PHP is supported through the Apache module. If you are compiling and installing PHP from source, if you want Apache support

In PHP. / configure steps you need to specify the -- with - apxs2 = / usr/local/apache2 bin/apxs said tells the compiler through

Apache's mod_php5/apxs provides the resolution of PHP5; And in the last step make install we'll see that the library will be dynamically linked

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

Statement to dynamically load the libphp5.so module to achieve Apache support for php.

2.2 Nginx + FastCGI mode

        Nginx is completely lightweight and must be parseable with the third FastCGI processor, so Nginx is

Very flexible, it can be connected to any 3rd party processor implementation that provides parsing to enable parsing of PHP (easy to set in nginx.conf).

        Nginx can use spwan-fcgi. You needed to install lighttpd in earlier versions, but you can install spawn-fcgi directly after version 9.10.

Now there is a new 3rd PHP FastCGI processor, called PHP-FPM, which can be read 1. This article is based on the spawn-fcgi implementation

PHP module support.

2.3 installation FastCGI

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

Is separated into individual packages.

    (1) the online installation command using apt-get is as follows:

                $sudo apt-get install spawn-fcgi

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

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

After unzipping                , enter the directory and execute the following installation command:

                $./configure

                $make

                $make install

              after installation, spawn - fcgi command can be directly used, its executable file in/usr local/bin/spawn - fcgi.

3. Nginx installation

3.1 installation Nginx

        (1) online installation

                  $sudo apt-get install nginx

The version of           Nginx is 1.2.1

The file structure of           ubuntu after installing Nginx is roughly:

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

          startup program file at /usr/sbin/nginx

          logs are in /var/log/nginx, access.log and error.log

          and have been in/etc/init d created under/startup scripts nginx

          default directory of the virtual host Settings in the/usr/share/nginx/www

      (2) source code installation

          download address: http: / / nginx org/download /

          what I downloaded here is nginx-1.3.9.tar gz. The installation process is simple, as follows:

          $./configure

          $make

          $make install

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

The nginx startup file is the nginx file in the sbin directory.

3.2 start Nginx

    (1) the online installation startup process

          $sudo /etc/init.d/nginx start

    (2) the startup process for the source code installation

          $cd /usr/local/nginx

          $sbin/nginx

          then you can access http://localhost/, 1 cut normal! If you can't access it, don't go ahead and see why,

We'll continue after that.

          if you have Apache installed on your machine at the same time, the access method above will not work, and nginx may not start, this is

Because they all use port 80. We have changed the port of nginx to 8080,

Here we mainly modify the configuration file nginx, nginx.conf, and put 1 down to this 1 line

            listen 80;

     

            listen 8080;

      then you can access http://localhost:8080/.

3.3 install PHP and MySQL

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

3.4 test Nginx's support for PHP

      (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

When an           spawn fcgi startup error occurs, check whether php-cgi is installed. If so, install php5-cgi.

          $sudo apt-get install php5-cgi

      (3) test

            open http: / / localhost phpinfo php

4. Nginx configuration

        Nginx configuration file is/etc nginx/nginx conf, including 1 set up some necessary parameters, we found this statement:

        include /etc/nginx/sites-enabled/*

As can be seen from        , the /etc/nginx/sites/enabled/default file is also a core configuration file, which contains the main configuration information.

Such as server with directory, server name, location information and server information.

        for nginx source installation, configuration file is/usr/local/nginx/conf/nginx conf.

      the following mainly explains the matching rules of location:

The directive     (1) = prefix matches this query exactly. If found, stop searching.

    (2) the remaining regular string, the longest match takes precedence. If the match USES the ^~ prefix, the search stops.

    (3) regular expression, the first match is used in the order specified in the configuration file.

    (4) this result is used if step 3 produces a match. Otherwise use the match result from step 2.

      can use regular strings and regular expressions in location.

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

            (1) - * prefix select a case-insensitive match

            (2) ~   select case-sensitive matching

      example:

        location = / {

# only matches/queries.

[configuration A]
        }

location / {

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

                      # but the regular expression rule and the long block rule will be first matched with the query.

[configuration B]

}

location ^ ~ / images / {

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

                  # any regular expression will not be tested.

[configuration C]

}

location ~ * \. (gif | jpg | jpeg) ${

                  # matches any request ending in gif, jpg or jpeg.

                # however, all /images/ directory requests will use Configuration C.

  [configuration D]

}

Here you also need to have a definite understanding of regular expressions!!


Related articles: