search
HomeOperation and MaintenanceNginxHow to install and configure Nginx in Ubuntu

How to install and configure Nginx in Ubuntu

May 15, 2023 pm 06:07 PM
nginxubuntu

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 in Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

Using NGINX Unit: Deploying and Managing ApplicationsUsing NGINX Unit: Deploying and Managing ApplicationsApr 22, 2025 am 12:06 AM

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX vs. Apache: A Comparative Analysis of Web ServersNGINX vs. Apache: A Comparative Analysis of Web ServersApr 21, 2025 am 12:08 AM

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX Unit's Advantages: Flexibility and PerformanceNGINX Unit's Advantages: Flexibility and PerformanceApr 20, 2025 am 12:07 AM

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.