Home > Operation and Maintenance > Nginx > How to install and configure Nginx under Ubuntu

How to install and configure Nginx under Ubuntu

王林
Release: 2023-05-17 11:01:34
forward
1229 people have browsed it

1.nginx introduction

nginx is a very lightweight http server, nginx, which is pronounced as "engine x", is a high-performance http and

Reverse proxy server is also an imap/pop3/smtp proxy server.

2. PHP support

Currently there are three types of support for PHP by various web servers:

(1) Built-in through the web server Modules are implemented, such as apache's mod_php5, and similar apache's built-in mod_perl

can support perl.

(2) Implemented through cgi, this is just like the previous cgi of perl. The disadvantage of this method is poor performance, because every time the server encounters

these scripts, the script needs to be restarted The parser executes the script and returns the results to the server;

On the other hand, it is not very safe; this aspect is almost rarely used.

(3) The latest one is called fastcgi. The so-called fastcgi is an improvement on cgi. It generally adopts a c/s structure. Generally, the script processor

will start one or more daemon processes. Every time the web server encounters a script, it is directly delivered to the fastcgi process for execution, and then

Return the result (usually html) to the browser.

2.1 apache mod_php mode

We have been using the classic apache mod_php for a long time.

Apache supports PHP through Apache modules. If you compile and install php from source code, if you want apache to support

php, you need to specify --with-apxs2=/usr/local/apache2/bin/apxs in the ./configure step to tell the compiler to pass

apache's mod_php5/apxs provides parsing of php5; and in the last step of make install, we will see that the dynamic link library

libphp5.so is copied to the installation directory of apache2 modules directory, and you also need to add the loadmodule

statement in the httpd.conf configuration file to dynamically load the libphp5.so module to realize apache's support for php.

2.2 nginx fastcgi mode

nginx is completely lightweight and must use a third-party fastcgi processor to parse php, so in fact it seems like this nginx is

very flexible. It can connect to any third-party parsing processor to realize the parsing of PHP (it is easy to set up in nginx.conf).

nginx can use spwan-fcgi. In earlier versions, lighttpd needs to be installed, but after version 9.10, spawn-fcgi can be installed directly.

Now there is a new third-party PHP fastcgi processor called php-fpm, you can learn about it. This article is based on spawn-fcgi to implement support for the

php module.

2.3 Install fastcgi

The file /usr/bin/spawn-fcgi is used to manage fastcgi. It originally belongs to the lighttpd package, but after 9.10, spawn-fcgi

is separated into separate packages.

(1) Use apt-get online installation command as follows:

$sudo apt-get install spawn-fcgi

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

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

$./configure

$make

$make install

After installation, the spawn-fcgi command can be used directly. Its executable file is in /usr/local/bin/spawn-fcgi.

3.nginx installation

3.1 Install nginx

(1) Online installation

$sudo apt-get install nginx

The version of nginx is 1.2.1

The file structure after installing nginx on ubuntu is roughly:

All configuration files are in /etc/nginx , and each virtual host has been arranged under /etc/nginx/sites-available

The startup program file is in /usr/sbin/nginx

The log is placed in /var/log /nginx, they are access.log and error.log

and the startup script nginx

has been created under /etc/init.d/. The default virtual host directory is set in /usr/share/nginx/www

(2) Source code installation

Download address:

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

$./configure

$make

$make install

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

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

3.2 Start nginx

(1) Startup process of online installation

$sudo /etc/init.d/nginx start

(2) Startup process of source code installation

$cd /usr/local/nginx

$sbin/nginx

Then you can access, http:/ /localhost/ , everything works! If you can't access it, don't continue yet, find out what the reason is, and

#solve it before continuing.

If your machine has apache installed at the same time, the above access method cannot be used, and nginx may not be started. This is

because they all use port 80. Here we change the port of nginx to 8080.

Here we mainly modify the nginx configuration file nginx.conf. Change this line

listen 80;

to

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 ; , 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

The configuration file of nginx is /etc/nginx/nginx.conf, which sets some necessary parameters. We found statements like this:

include / etc/nginx/sites-enabled/*

It can be seen that the /etc/nginx/sites-enabled/default file is also a core configuration file, which contains the main configuration information,

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

For nginx installed from source code, the configuration file is /usr/local/nginx/conf/nginx.conf.

The following mainly explains the matching rules of location:

(1) = The prefix command strictly matches this query. If found, stop searching.

(2) For the remaining regular strings, the longest match will be used first. If the match uses the ^~ prefix, the search stops.

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

(4) If a match occurs in the third step, use this result. Otherwise, the matching result from the second step is used.

Regular strings and regular expressions can be used in location.

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

(1)~* Prefix selects case-insensitive matching

(2)~ Selects size-sensitive Write matching

Example:

location = / {

  # Only matches / query.

   [ configuration a ]

  }


  location / {

   # Matches any query because all requests begin with /.

              # But regular expression rules and long block rules will be given priority to match the query.

    [ configuration b ]

  }

  Location ^~ /images/ {

   # Match any query starting with /images/ and stop the search .

              # Any regular expression will not be tested.

   [configuration c]

 }

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

     # Matches any item starting with gif, jpg or request ending in jpeg.

          # However all requests to the /images/ directory will use configuration c.

  [ configuration d ]

  }

The above is the detailed content of How to install and configure Nginx under Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template