Nginx is simple webserver yet powerful. It is widely know for stable webserver. If it is well configured, nginx rarely have spike in the cpu load and have low memory consumption. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption.
In this article i want to share how to install and configure your Ubuntu server to use Nginx as web server, PHP5 support (using fastcgi) and MySQL support.
Install MySQL server
- Open your ssh terminal.
- Install MySQL:
sudo apt-get install mysql-server mysql-client
Install and Configure Nginx
- Still in your ssh terminal.
- Update your repositories list
sudo apt-get update
- Install nginx:
sudo apt-get install nginx
- Nginx configuration file is under /etc/nginx/sites-available/default, and it should be change to:
server { listen 80; server_name localhost; access_log /var/log/nginx/localhost.access.log; ## Default location location / { root /var/www; index index.php; } ## Images and static content is treated different location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ { access_log off; expires 30d; root /var/www; } ## Parse all .php file in the /var/www directory location ~ .php$ { fastcgi_split_path_info ^(.+.php)(.*)$; fastcgi_pass backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; include fastcgi_params; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_intercept_errors on; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; } ## Disable viewing .htaccess & .htpassword location ~ /.ht { deny all; } } upstream backend { server 127.0.0.1:9000; }
Install and Configure PHP5 with Fastcgi
- Install spawn-fcgi with following command:
sudo apt-get install spawn-fcgi
- Install PHP:
sudo apt-get install php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql
- Now let’s write a script which will spawn FastCGI PHP processes on a unix domain socket. We create file create file /usr/sbin/fastcgi-php.
sudo touch /usr/sbin/fastcgi-php
then write this into the file:
#!/bin/sh /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi
- To make it work at startup we need now to create an init script:
sudo touch /etc/init.d/init-fastcgi
Then write the following:
#!/bin/bash PHP_SCRIPT=/usr/bin/php-fastcgi RETVAL=0 case "$1" in start) $PHP_SCRIPT RETVAL=$? ;; stop) killall -9 php RETVAL=$? ;; restart) killall -9 php $PHP_SCRIPT RETVAL=$? ;; *) echo "Usage: php-fastcgi {start|stop|restart}" exit 1 ;; esac exit $RETVAL
Give the script execute permission:
sudo chmod 755 /etc/init.d/init-fastcgi #then run it /etc/init.d/init-fastcgi start
Now let’s start it on boot:
sudo update-rc.d init-fastcgi defaults
- Restart Nginx:
sudo /etc/init.d/nginx restart
- Test it with phpinfo(). If you see the result now everything is okay, but if you don’t please revise the steps you have done.
PS: this steps work for me on my machine with Ubuntu 10.04 server edition. I’m not guarantee that this will work for you too, but that’s the basic to install Nginx, PHP, MySQL On Ubuntu. If you are not successful maybe you just need to check the configuration to get it working. I cannot get it run in first try. So keep trying, it’s a learning process.
Related article:
[HowTo] Install Apache, PHP, MySQL, and PHPMyAdmin on Ubuntu
I think you need to do a chmod 755 /usr/sbin/fastcgi-php
The script should have PHP_SCRIPT=/usr/sbin/php-fastcgi not PHP_SCRIPT=/usr/bin/php-fastcgi
Actually, that line should be PHP_SCRIPT=/usr/sbin/fastcgi-php
Hi,
I want to run mysql in terminal (Ubuntu 10.10).
I have installed lampp.
i had run the following command.
apt-get install mysql-client mysql-server.
when i am giving following command in terminal
root@arreys-17:~# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 33
Server version: 5.0.75-0ubuntu10.5 (Ubuntu)
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
+——————–+
2 rows in set (0.00 sec)
mysql>
But i want to access mysql (lampp mysql database and table) in terminal of ubuntu 10.10
We can do this. If yes please tell me how to do?
Thanks
to connect to mysql: mysql -h <host> -u <username> -p <password> <database_name>
Thanks
You're also going to need some kind of monitoring process to restart the php processes when they fail, such as superivisord. Otherwise you get 502 bad gateway errors from time to time.
These pages worked for me:
http://sachachua.com/blog/2010/06/using-supervisord-for-nginxfastcgiphp/http://agiletesting.blogspot.com/2010/06/setting-up-php5fastcgi-with-nginx.html
In order to do this you'll also need to remove the init-fastcgi file, such as:
mv /etc/init.d/init-fastcgi ~