Announcement

Collapse
No announcement yet.

How to Install Nginx on Ubuntu 12.04

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Install Nginx on Ubuntu 12.04

    How to Install Nginx on Ubuntu 12.04


    The tutorial is done on Ubuntu 12.04 but you may use newer version of the distro with very little adjustment. The tutorial below shows you how to install Nginx, PHP5 (php-fpm), and MySQL or simply known as LNMP or LEMP stack.
    How to Install Nginx
    Code:
    Step 1.
    Update apt-get package:
    
    apt-get update
    
    Step 2.
    Lets install Nginx using command:
    
    apt-get install nginx
    
    Step 3.
    Start Nginx service for the very first time:
    
    service nginx start
    
    Step 4.
    Now you can install PHP
    
    apt-get install php5-fpm
    
    Step 5.
    Configure PHP:
    
    nano /etc/php5/fpm/php.ini
    
    Step 6.
    Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.
    
    cgi.fix_pathinfo=0
    
    Save and Exit.
    info:
    If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative.

    Step 7.
    Make another small change in the php5-fpm configuration.Open up
    Code:
     www.conf
    Code:
    nano /etc/php5/fpm/pool.d/www.conf
    
    Step 8.
    Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.
    
    listen = /var/run/php5-fpm.sock
    
    Save and Exit (Control+O then Control+X)
    
    Step 9.
    Restart PHP-fpm:
    
    service php5-fpm restart
    
    Step 10.
    Install MySQL database service:
    
    apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
    Step 11.
    MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell. Once you have installed MySQL, we should activate it with this command:

    mysql_install_db

    Step 12.
    Finish up by running the MySQL set up script:

    Code:
    /usr/bin/mysql_secure_installation
    Step 13.
    The prompt will ask you for your current root password. Type it in. Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps. It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.

    Step 14.
    How to configure Nginx on Ubuntu. First, edit Nginx virtual hosts file:
    Code:
    nano /etc/nginx/sites-available/default
    
    Step 15.
    Made necessary configuration according your server and site. Example of Nginx virtual hosts file:
    
    [...]
    server {
            listen   80;
         
            # sometimes it is /usr/share/nginx/html
            root /usr/share/nginx/www;
            index index.php index.html index.htm;
    
            server_name example.com;
    
            location / {
                    try_files $uri $uri/ /index.html;
            }
    
            error_page 404 /404.html;
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                  root /usr/share/nginx/www;
            }
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            location ~ .php$ {
                    #fastcgi_pass 127.0.0.1:9000;
                    # With php5-fpm:
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
                    
            }
    
    }
    [...]
    
    About the vhosts config above:
    - Add index.php to the index line.
    - Change the server_name from local host to your domain name or IP address
    (replace the example.com in the configuration)
    - Change the correct lines in “location ~ \.php$ {“ section
    Once done, save and exit Nano editor.

    Step 16.
    That’s it.

    Your default folder where you can add web files is located at /usr/share/nginx/html or /usr/share/nginx/www. Go ahead create your very first index.html file, place it there and try accessing it from your web browser via http://ip.address/
    Last edited by kuldeep; 02-25-2015, 10:47 PM.
Working...
X