Installing NGINX, PHP, and MySQL on Ubuntu 10.04 LTS using php-cgi
조회 수 6105 추천 수 0 2013.04.11 15:58:17The how to article below describes installing the usual components for the LAMP stack (Linux, Apache, MySQL, and PHP), however the Apache part is exchanged for Nginx.
Make sure the apt sources are up to date before installing:
$ sudo apt-get update
MySQL Configuration
Install MySQL:
$ sudo apt-get install -y mysql-server mysql-client
You will be asked to enter a “root” password for the MySQL server. Generate a strong password, type it in, and keep note of it for future reference.
PHP Configuration
To use PHP with Nginx, you require the PHP5-CGI package. Install PHP with php5-cgi and any other modules that you may require:
$ sudo apt-get install -y 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-ps php5-pspell php5-recode \ php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json
Note: You can modify the above command to add/remove individual PHP packages if needed. The php5-cgi package is however required when used with Nginx.
Download and set up the php-fastcgi init script for Ubuntu, using the following commands:
$ cd /etc/init.d $ sudo wget -O php-fastcgi http://www.networkinghowtos.com/wp-content/uploads/scripts/php-fastcgi $ sudo chmod +x php-fastcgi $ sudo update-rc.d -f php-fastcgi defaults
The source for this file and more details can be found on the following page:
http://www.networkinghowtos.com/howto/ubuntu-php-fastcgi-init-script/
Start (or Restart) PHP Fast CGI:
$ sudo /etc/init.d/php-fastcgi restart
NGiNX Configuration
Install Nginx:
$ sudo apt-get install -y nginx
Edit the Nginx configuration file so that it knows what to do with .PHP files:
$ sudo nano /etc/nginx/sites-available/default
There will be a section with the heading starting with “pass the PHP scripts to FastCGI server” as seen below:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #fastcgi_pass 127.0.0.1:9000; #fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #includefastcgi_params; #}
Uncomment the lines as shown below, and modify the “fastcgi_param” line to reflect your website folder path. In my example I am using /var/www.
Also take note of the space added to the “include” line. For some reason the example code in the config file was missing the space. Add this space (or tab) as per the below example.
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; include fastcgi_params; }
Save and exit the editor.
Please note that there could be potential security issues with this generic base configuration (particularly if you are running a publicly accessible website, and allow file uploads). If you would like further information on this, please read the blog post at https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/ and also read through the Nginx documentation.
Start Nginx:
$ sudo /etc/init.d/nginx start
Conclusion and Testing
Create a .php file containing the following PHP code and save it in your website folder and call it test.php (/var/www/test.php in my example.):
1 |
<?php |
2 |
phpinfo(); |
3 |
?> |
Load up your site in a browser, and add the file /test.php to the url, and make sure it comes up with the usual PHP information page, and not just the source code for the .php file you created.
You should now have a working Nginx, PHP, and MySQL stack. Read the Nginx documentation for more detailed configuration options.
The Nginx documentation can be found on the Nginx website at http://nginx.org/