Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and configure Nginx in Ubuntu

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to install and configure Nginx in Ubuntu". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to install and configure Nginx in Ubuntu".

1.nginx introduction

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

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

two。 Support for php

Currently, there are three types of php support for various web servers:

(1) implemented through the built-in modules of the web server, such as apache's mod_php5, similar to apache's built-in mod_perl

Perl can be supported.

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

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; this aspect is rarely used.

(3) the latest one is called fastcgi. The so-called fastcgi is the improvement of cgi. It generally adopts the structure of cjump s and the general script processor.

One or more daemon processes are started, and each time the web server encounters a script, it delivers it directly to the fastcgi process for execution, and then

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

2.1 apache+mod_php mode

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

Apache's support for php is supported through apache's modules. If you install php from source code compilation, if you want apache to support

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

Apache's mod_php5/apxs to provide parsing of php5; and in the final step of make install, we will see that the dynamic link library

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

Statement to dynamically load the libphp5.so module in, thus realizing apache's support for php.

2.2 nginx+fastcgi mode

Nginx is completely lightweight, and php can only be parsed with the help of a third-party fastcgi processor, so in fact, nginx is

Very flexible, it can be connected to any third-party processor that provides parsing to achieve parsing of php (which is easy to set up in nginx.conf).

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

Now there is a new third-party php fastcgi processor called php-fpm, which you can learn about. This paper is based on the implementation of spawn-fcgi.

Support for php module.

2.3 install fastcgi

/ usr/bin/spawn-fcgi this file to manage fastcgi, it used to belong to the lighttpd package, but after 9.10, spawn-fcgi

Separated into separate bags.

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

$sudo apt-get install spawn-fcgi

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

After unzipping, go to the directory and execute the following installation command:

$. / configure

$make

$make install

After installation, the spawn-fcgi command can be used directly, and 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

Version of nginx is 1.2.1

The file structure of ubuntu after installing nginx is roughly as follows:

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

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

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

And the startup script nginx has been created under / etc/init.d/

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

(2) Source code installation

Download address:

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

$. / configure

$make

$make install

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

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

3.2 start nginx

(1) the startup process of online installation

$sudo / etc/init.d/nginx start

(2) the startup process of source code installation

$cd / usr/local/nginx

$sbin/nginx

Then you can visit, http://localhost/, everything is fine! If you can't access it, don't go on and see why.

Don't continue until it's settled.

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

Because they all use port 80. Here we modify the port of nginx to 8080

Here we mainly modify the configuration file nginx.conf of nginx to change this line

Listen 80

Modify to

Listen 8080

Then you can visit, http://localhost:8080/.

3.3 install php and mysql

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

3.4Test 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 there is an error in spawn-fcgi startup, check to see if php-cgi is installed, and if not, install php5-cgi.

$sudo apt-get install php5-cgi

(3) testing

Open http://localhost/phpinfo.php

4.nginx configuration

The configuration file for nginx is / etc/nginx/nginx.conf, which sets some necessary parameters, and we find a statement like this:

Include / etc/nginx/sites-enabled/*

You can see 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 in the source code, the configuration file is / usr/local/nginx/conf/nginx.conf.

The following mainly describes the matching rules of location:

(1) the instruction with the = prefix strictly matches this query. If you find it, stop searching.

(2) for the rest of the regular strings, the longest matching takes precedence. If the match uses the ^ ~ prefix, the search stops.

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

(4) if the third step produces a match, this result is used. Otherwise, the matching result of 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 selection is case-insensitive matching

(2) ~ choose case-sensitive matching

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 preferred to match the query.

[configuration b]

}

Location ^ ~ / images/ {

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

Any regular expressions will not be tested.

[configuration c]

}

Location *\. (gif | jpg | jpeg) ${

# matches any request that ends in gif, jpg, or jpeg.

# however, all requests for the / images/ directory will use configuration c.

[configuration d]

}

At this point, I believe you have a deeper understanding of "how to install and configure Nginx in Ubuntu". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report