Home >Backend Development >PHP Tutorial >Detailed explanation of configuring nginx+php+mysql under ubuntu, ubuntunginx_PHP tutorial

Detailed explanation of configuring nginx+php+mysql under ubuntu, ubuntunginx_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:53:50829browse

Detailed explanation of configuring nginx+php+mysql under ubuntu, ubuntunginx

1. Update

www.jb51.net

Copy code The code is as follows:
sudo apt-get update

2. Install nginx

Copy code The code is as follows:
sudo apt-get intsall nginx

The file structure after Ubuntu installation is roughly:

* All configuration files are under /etc/nginx, and each virtual host has been arranged under /etc/nginx/sites-available www.jb51.net

*Program files are in /usr/sbin/nginx * Logs are placed in /var/log/nginx

* and have created the startup script nginx
under /etc/init.d/

* The default virtual host directory is set to /var/www/nginx-default

You can start nginx below to see the effect (please make sure that no other services are using port 80):

Copy code The code is as follows:
sudo /etc/init.d/nginx start

# or simply

Copy code The code is as follows:
service nginx start

Then open the browser and check http://localhost/ to see if you see "Welcome to nginx!" If you see it, the installation is successful.

Of course, basically, there will be no problems with this. If the operation fails, you can first

Copy code The code is as follows:
sudo killall apache2

Kill the apache process

3. Install php

Copy code The code is as follows:
sudo apt-get install php5 php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5- snmp php5-tidy php5-xmlrpc php5-sqlite php5-xsl

4. Install spawn-fcgi

Why should you install spawn-fcgi? It is used to control the php-cgi process to prevent the process from crashing or the efficiency of a single process being too low.

Many people on the Internet say that to use spawn-fcgi, you must install lighttpd. In fact, it is not necessary. You can install spawn-fcgi directly

Run:

Copy code The code is as follows:
sudo apt-get install spawn-fcgi

5. Configuration

The next step is the most troublesome configuration.

Configure Nginx and spawn-fcgi to run together

(1). At the end of the /etc/nginx/fastcgi_params file, add a line, you can use

sudo vi /etc/nginx/fastcgi_params 

Join this trip:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

(2). In addition, you need to find the cgi.fix_pathinfo option in the PHP-CGI configuration file (this configuration file is located in /etc/php5/cgi/php.ini on Ubuntu) and modify it to:

cgi.fix_pathinfo=1;

This way php-cgi can use the SCRIPT_FILENAME variable normally.

(3). Open the /etc/nginx/sites-available/default file in

server {
listen 80;
server_name localhost;

Add the absolute address of the web root directory below. The default address of nginx is used here

root /var/www/nginx-default

That is, root and server_name are at the same level, which is equivalent to the default directory of apache

Without this, when executing the php file, it is easy to prompt "No input file specified".

I went through a lot of circles here before I found the problem. Then I modified it

#location ~ .php$ {
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#includefastcgi_params;
#}

was changed to

location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params; #Include fastcgi parameter file address

6. Start the fast_cgi process

sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

7. Set up the fastcgi process at boot and start it

sudo vi /etc/rc.local

Add next line

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

If you open a php file and it appears: No input file specified, please check the configuration of php.ini

cgi.fix_pathinfo=1

doc_root=

Also, each virtual machine must set up a different directory according to its different virtual machines, and ensure that the path is correct.

Check the configuration file under /etc/nginx/sites-available. The server contains root and address, not the root in location

Start

fast-cgisudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

The meaning of parameters is as follows

* -f specifies the execution program location of the process that calls FastCGI, and sets it specifically according to the PHP installed on the system
* -a bind to address addr
* -p bind to port port
* -s Path path bound to unix socket
* -C specifies the number of FastCGI processes generated, the default is 5 (only for PHP)
* -P specifies the PID file path of the generated process
* What identity does -u and -g FastCGI use to run (-u user-g user group)? You can use www-data under Ubuntu. Others are configured according to the situation, such as nobody, apache, etc. You can now put a probe in the web root directory Or php file to test it

8. Install mysql

sudo apt-get install mysql-server mysql-client

You will be prompted to enter the Root user password in the middle, just enter it in sequence.

Start MySQL

sudo /etc/init.d/mysql start

Test whether the mysql service is normal:

Run

mysql -uroot -p

Enter mysql password

show databases;

If you see the following content

| Database |
| information_schema |
| mysql |

Then mysql has been installed correctly.

At this point, the installation of nginx+php+mysql under ubuntu is completed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1053151.htmlTechArticleDetailed explanation of configuring nginx+php+mysql under ubuntu, ubuntunginx 1. Update www.jb51.net Copy the code code as follows: sudo apt-get update 2. Install nginx. Copy the code as follows: sudo apt-get in...
Statement:
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