Installation and configuration of Nginx in Ubuntu
- 2020-05-06 12:08:23
- OfStack
1.Nginx introduces
Nginx is a very lightweight HTTP server, Nginx, which is pronounced "engine X", is a high-performance HTTP and
The reverse proxy server is also an IMAP/POP3/SMTP proxy server.
2.
support for PHPcurrently supports PHP in three different web servers:
(1) is implemented by web server built-in modules, such as mod_php5 of Apache, and mod_perl
of Apacheperl can be supported.
(2) is implemented with CGI, which is similar to CGI before 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 then return the results to the server.
The other side is less safe; This aspect is rarely used anymore.
(3) the latest is called FastCGI. FastCGI is an improvement on CGI. It generally USES C/S structure, the general script processor
One or more daemon processes are started, and each time the web server encounters a script, it is delivered 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 used the classic Apache+mod_php for a long time.
Apache support for PHP is supported through the Apache module. If you compile and install PHP from source code, you want Apache to support
In PHP. / configure steps you need to specify the -- with - apxs2 = / usr/local/apache2 bin/tells the compiler through
apxs saidApache mod_php5/apxs to provide an PHP5 resolution; And in the last step make install we will see that
library will be dynamically linkedCopy libphp5.so to the modules directory of the apache2 installation directory and add LoadModule
to the httpd.conf configuration fileStatement to dynamically load the libphp5.so module to achieve Apache support for php.
2.2 Nginx+FastCGI mode
Nginx is completely lightweight and can only be resolved with a third-party FastCGI processor, so Nginx is actually
Very flexible, it can be connected to any third-party processor that provides the parsing to achieve the parsing of PHP (easy to set up in nginx.conf).
Nginx can use spwan-fcgi. You need to install lighttpd in earlier versions, but after version 9.10 you can install spawn-fcgi directly.
Now there is a new third party PHP FastCGI processor called PHP-FPM. This article is based on spawn-fcgi implementation on
PHP module support.
2.3 install FastCGI
/usr/bin/ spawn-fcgi this file is managed by FastCGI, which originally belongs to the lighttpd package, but after 9.10, spawn-fcgi
Is separated into individual packages.
The online installation command for (1) using apt-get is as follows:
$sudo apt-get install spawn-fcgi
The (2) source code is installed as follows. The download address is
http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
After unzipping , go to the directory and execute the following installation command:
$./configure
$make
$make install
after installation, spawn - fcgi command can be directly used, its executable file in/usr local/bin/spawn - fcgi.
3.Nginx install
3.1 install Nginx
(1) install
online$sudo apt-get install nginx
Nginx is 1.2.1
ubuntu
all configuration files are under /etc/nginx, and each virtual host is already under /etc/nginx/ sites-available
start the program files in/usr/sbin/nginx
logs are in /var/log/nginx, access.log and error.log
and have been in/etc/init d created under/startup scripts nginx
default directory of the virtual host Settings in the/usr/share/nginx/www
(2) source code installation
download address: http: / / nginx org/download/
I have downloaded nginx-1.3.9. tar. gz
$./configure
$make
$make install
After the installation is successful, nginx is placed in the /usr/local/nginx directory. The main configuration file is nginx.conf,
in the conf directoryThe nginx startup file is the nginx file in the sbin directory.
3.2 starts Nginx
(1) online installation of the startup process
$sudo /etc/init.d/nginx start
(2) source code installation startup procedure
$cd /usr/local/nginx
$sbin/nginx
http://localhost/, all right! If you can't access it, don't go ahead and see why,
We'll continue after that.
if you have Apache installed on your machine at the same time, the above access method will not work, and nginx may not boot, this is
Because they're all using port 80. We have changed the port of nginx to 8080 and
The main modification here is nginx.conf's configuration file nginx.conf
listen 80;
changed 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 tests Nginx's support for PHP
(1) restart nginx:
$/etc/init.d/nginx restart
(2) starts FastCGI:
$spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi
If there is an error on the boot of spawn-fcgi, check to see if php-cgi is installed. If so, install php5-cgi.
$sudo apt-get install php5-cgi
(3) test
open http: / / localhost phpinfo php
4.Nginx is configured with
Nginx configuration file is/etc nginx/nginx conf, some of which set up the necessary parameters, we found this statement:
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 with directory, server name, location information and server information.
for nginx source installation, configuration file is/usr/local/nginx/conf/nginx conf.
the following mainly explains the matching rules of location:
The directive (1) = prefix matches this query exactly. If found, stop the search.
(2) the rest of the regular string, the longest match takes precedence. If the match USES the ^~ prefix, the search stops.
(3) regular expressions, in the order in the configuration file, are used for the first match.
(4) this result is used if the third step produces a match. Otherwise use the second step's match result.
USES regular strings and regular expressions in location.
If you use regular expressions, you must use the following rules:
(1) ~* prefix select case-insensitive match
(2) ~ select case-sensitive match
example:
location = / {
Match/query only.
[configuration A]
location / {
# matches any query because all requests begin with /.
# but the regular expression rule and the long block rule will be matched first and 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 request ending in gif, jpg, or jpeg. # however, all requests for /images/ directories will use Configuration C.
[configuration D]
}
Here you also need to have a certain understanding of regular expressions!!
}