Install and configure nginx, php, and mysql on ubuntu

Source: Internet
Author: User
This article describes how to install and configure nginx + php + mysql in ubuntu. For more information, see tung.pdf

1. Update

1 Sudo apt-get update

2. install nginx

1 Sudo apt-get intsall nginx

After Ubuntu is installed, the file structure is roughly as follows:
* All configuration files are under/etc/nginx, and each virtual host is already under/etc/nginx/sites-available.
* The program file is stored in the/usr/sbin/nginx * log in/var/log/nginx.
* The startup script nginx has been created under/etc/init. d /.
* The default virtual host directory is/var/www/nginx-default.

You can start nginx to see the effect (make sure there are no other services in use on port 80 ):

1 Sudo/etc/init. d/nginx start

# Or simply

1 Service nginx start

Open the browser and check http: // localhost/to see if "Welcome to nginx!" is displayed !" If yes, the installation is successful.
Of course, basically, there is no problem with this Part. if it fails to run, you can first

1 Sudo killall apache2

Kill apache process
3. install php

Sudo apt-get install php5 php5-cgi php5-mysql php5-curl php5-gd php5-idn php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode

4. install spawn-fcgi
Why is spawn-fcgi installed? it is used to control the php-cgi process to prevent process crashes or the efficiency of a single process is too low.
Many people on the Internet say that to use spawn-fcgi, lighttpd must be installed. In fact, it is not necessary to install spawn-fcgi directly.
Run:

1 Sudo apt-get install spawn-fcgi

5. configuration
The next step is the most frustrating configuration.
Configure Nginx and spawn-fcgi to run together
(1) add a line at the end of the/etc/nginx/fastcgi_params file. you can use

1 Sudo vi/etc/nginx/fastcgi_params

Join this line:

1 Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;

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

1 Cgi. fix_pathinfo = 1;

In this way, the SCRIPT_FILENAME variable can be used normally in php-cgi.
(3) open the/etc/nginx/sites-available/default file in

123 Server {listen 80; server_name localhost;

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

1 Root/var/www/nginx-default

That is, the root and server_name are at the same level, which is equivalent to the default directory of apache.
If this is not found, the system prompts "No input file specified" when executing the php file ".
I found the problem only after a large circle. then I modified it.

123456 # Location ~ . Php $ {# fastcgi_pass 127.0.0.1: 9000; # fastcgi_index index. php; # fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name; # inclustcgi_params ;#}

Modify

1234 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; # address of the parameter file containing fastcgi
6. start the fast_cgi process.

1, 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 boot to start fastcgi process and start

1 Sudo vi/etc/rc. local

Add next line

1 /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 No input file specified is displayed when you open the php file, check the php. ini configuration.

1 Cgi. fix_pathinfo = 1
1 Doc_root =

In addition, you must set different directories for each virtual machine based on its own virtual machine to ensure that the path is correct.
Check the configuration file in/etc/nginx/sites-available. The server contains the root and root address, not the root address in the location.
Start

1 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 parameter description is as follows:
*-F specifies the location of the execution program of the process that calls FastCGI, which is set according to the PHP installed on the system
*-A is bound to the addr address.
*-P bind to the port
*-S is the path to bind to the unix socket
*-C indicates the number of FastCGI processes generated. the default value is 5 (for PHP only)
*-P specifies the PID file path of the generated process
* What identity does-u and-g FastCGI use (-u user-g user group) to run? www-data can be used in Ubuntu, and others can be configured as needed, for example, nobody, apache, etc. now you can test a probe or php file in the web root directory.
8. install mysql

1 Sudo apt-get install mysql-server mysql-client

In the middle, you will be prompted to enter the Root user password and enter it in sequence.
Start MySQL

1 Sudo/etc/init. d/mysql start

Test whether the mysql service is normal:
Run

1 Mysql-uroot-p

Enter the mysql password

1 Show databases;

If you see the following content
| Database |
| Information_schema |
| Mysql |
Mysql is correctly installed.
So far, nginx + php + mysql installation in ubuntu is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.