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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

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

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 solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to solve the problem of nginx cross-domain How to solve the problem of nginx cross-domain Apr 14, 2025 am 10:15 AM

There are two ways to solve the Nginx cross-domain problem: modify the cross-domain response header: add directives to allow cross-domain requests, specify allowed methods and headers, and set cache time. Use CORS modules: Enable modules and configure CORS rules that allow cross-domain requests, methods, headers, and cache times.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

See all articles