Home Operation and Maintenance Nginx How to install and configure Nginx in Ubuntu

How to install and configure Nginx in Ubuntu

May 15, 2023 pm 06:07 PM
nginx ubuntu

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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles