Why another web server : Take a quick look
Every process running on apache server can have only one client i.e each process can handle only one request at a time and that’s how most of the servers have been designed.The need of serving large number of requests is raising day by day. So, if we require 10,000 clients to connect, we must have 10,000 processes currently running on our web server.If such large number of simultaneous connections are established, you might be facing problems like deadlocks,memory issues and lack of hardware resources etc. Therefore, the problem of scalability of web server came into picture.
The web server scalability problem can be solved either by increasing the hardware capabilities (i.e memory, CPU, etc ) or by improving the web server architecture.The goal here is to improve the web server architecture.
Apache is based on process-driven architecture i.e it creates processes and threads to handle additional connections so the administrator has to configure the server to handle the maximum number of allowable processes.Too many processes can exhaust memory, resulting a server with degrading performance.Moreover,when the limit of the processes is reached it also refuses additonal connections.
On the other hand, Nginx works on event-driven architecture i.e it does not create new processes for each web request, instead the administrator configures how many worker processes to create for the main Nginx process and each worker can handle thousands of concurrent connections.Several benchmarking results indicates that when compared to Apache, Nginx is light-weighted and extremely fast for serving static pages.
How to install LEMP (Linux, Nginx, Mysql,Php) on Ubuntu 14.04 :
Run the following commands to install nginx :-
1 2 |
sudo apt-get update sudo apt-get install nginx |
Hit the URL to check if nginx has been successfully installed :-
1 |
http://server_domain_name_or_IP |
Install mysql using the following command :-
1 |
sudo apt-get install mysql-server |
Since Nginx does not contain native PHP processing like some other web servers, we will need to install php5-fpm, which stands for “fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing.
Run the following command :-
1 |
sudo apt-get install php5-fpm php5-mysql |
Open the main php5-fpm configuration file :-
1 |
sudo nano /etc/php5/fpm/php.ini |
In this file, we have to uncomment the parameter “cgi.fix_pathinfo” and set it to zero :-
1 |
cgi.fix_pathinfo=0 |
This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if a PHP file does not match exactly. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn’t be allowed to execute.
Restart php5-fpm :-
1 |
sudo service php5-fpm restart |
Now, we need to tell Nginx to use our Php processer. To enable php, we have to make the following changes in default Nginx server block configuration file :-
1 |
sudo nano /etc/nginx/sites-available/default |
The changes which we need to make are highlighted below :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index <span style="color: #ff0000">index.php</span> index.html index.htm; <span style="color: #ff0000">server_name server_domain_name_or_IP; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;</span> } } |
Restart Nginx :-
1 |
sudo service nginx restart |
Now, create a php file to test if the changes have been successfully done :-
1 |
sudo nano /usr/share/nginx/html/info.php |
1 2 3 |
<?php phpinfo(); ?> |
Hit the URL :-
1 2 |
http://server_domain_name_or_IP/info.php |
If you see a page that looks like this, you’ve set up PHP processing with Nginx successfully.