DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

How To Remove Apache, Set Up Nginx, PHP, Sql Buddy and Get Your Server Ready For Operation

As most of you know, a virtual private server (VPS) or a Dedicated server can be used for a multitude of things ranging from hosting voice communications and game servers to Virtual private network tunnels and TOR relays. But, there is one thing that they are both used for in abundance, and that is web serving. Whether it be a personal blog, or a corporate website, these tools and services are a vital part of the internet, and not to mention, fun as hell. Nginx alone is smaller in size than Apache, it multiplies the performance of your webapps or website immensely, by reducing RAM and CPU costs drastically. In this tutorial, I will outline and guide you to delete the default Apache2 (if it is already there) set up your PHP, MySQL, and Nginx, to get your server up and running on (in my opinion) the fastest web-server on the net. So let's begin shall we?

Note: This was done on a 512mb Ubuntu 12.04LTS VPS, but the commands should work and be compatible with pretty much most distributions. Also note, if you are wondering what the ”-y” means at some of the commands, it is just a means to avoid the prompt asking you if you want to install it.

Deleting Apache2

We need to remove Apache in order for us to work with our new toy un-conflicted. To do this, execute the following commands:

sudo apt-get remove --purge apache2 apache2-utils -y
sudo rm -rf /etc/apache2
Now that Apache is history, it is time to get our hands dirty.

Installing Nginx, PHP-FPM, MySQL & Sql Buddy

sudo apt-get install nginx -y
That installs nginx for you automatically. Now we need to do a bit of configuration! Yay! So we're going to make a backup of the config first, then edit it. So, execute these commands:
cd /etc/nginx
sudo cp nginx.conf nginx.conf.backup
sudo nano nginx.conf
This will take us into the almighty “nano” editor. My personal fave. If you don't already have nano (and most new installations of Ubuntu won't) execute this command:
sudo apt-get install nano
sudo apt-get update
Now we have the almighty command line text editor. Now that we have nanod into our config file for nginx, you're going to want it to look something like the following (you can tweak it to your liking as well)
user www-data;
 
# As a thumb rule: One per CPU. If you are serving a large amount
# of static files, which requires blocking disk reads, you may want
# to increase this from the number of cpu_cores available on your
# system.
#
# The maximum number of connections for Nginx is calculated by:
# max_clients = worker_processes * worker_connections
worker_processes 1;
 
# Maximum file descriptors that can be opened per process
# This should be > worker_connections
worker_rlimit_nofile 8192;
 
events {
    # When you need > 8000 * cpu_cores connections, you start optimizing
    # your OS, and this is probably the point at where you hire people
    # who are smarter than you, this is *a lot* of requests.
    worker_connections 8000;
}
 
error_log /var/log/nginx/error.log;
 
pid /var/run/nginx.pid;
 
http {
    charset utf-8;
 
    # Set the mime-types via the mime.types external file
    include mime.types;
 
    # And the fallback mime-type
    default_type application/octet-stream;
 
    # Click tracking!
    access_log /var/log/nginx/access.log;
 
    # Hide nginx version
    server_tokens off;
 
    # ~2 seconds is often enough for HTML/CSS, but connections in
    # Nginx are cheap, so generally it's safe to increase it
    keepalive_timeout 20;
 
    # You usually want to serve static files with Nginx
    sendfile on;
 
    tcp_nopush on; # off may be better for Comet/long-poll stuff
    tcp_nodelay off; # on may be better for Comet/long-poll stuff
 
    server_name_in_redirect off;
    types_hash_max_size 2048;
 
    gzipon;
    gzip_http_version 1.0;
    gzip_comp_level 5;
    gzip_min_length 512;
    gzip_buffers 4 8k;
    gzip_proxied any;
    gzip_types
        # text/html is always compressed by HttpGzipModule
        text/css
        text/plain
        text/x-component
        application/javascript
        application/json
        application/xml
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        application/vnd.ms-fontobject
        image/svg+xml
        image/x-icon;
 
    # This should be turned on if you are going to have pre-compressed copies (.gz) of
    # static files available. If not it should be left off as it will cause extra I/O
    # for the check. It would be better to enable this in a location {} block for
    # a specific directory:
    # gzip_static on;
 
    gzip_disable "msie6";
    gzip_vary on;
 
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
Now we have to change the default site configuration. Execute these 2 commands.
 sudo cp sites-available/default sites-available/default.backup
 sudo nano sites-available/default
Your configuration should look like this:
 server {
    listen 80 default; ## listen for ipv4; this line is default and implied
    listen [::]:80 default ipv6only=on; ## listen for ipv6
 
    # Make site accessible from http://localhost/ or server IP-address
    server_name localhost;
    server_name_in_redirect off;
 
    charset utf-8;
 
    access_log /usr/share/nginx/access.log;
    error_log /usr/share/nginx/error.log;
 
    root /usr/share/nginx/www;
    index index.php index.html index.htm;
 
    location / {
        # First attempt to serve request as file, then
        # as directory, then trigger 404
        try_files $uri $uri/ =404;
    }
}
Now that we have that completed, it is time to reload nginx.
sudo service nginx reload
Now we can test to see if nginx is working as intended by typing in the address bar of your browser of choice http://your-ip-here If everything is working correctly, you should get the nginx welcome page. Awesome! Now we need to get mysql up and running, along with SQL buddy or PhpMyAdmin.

Installing MySQL

sudo apt-get install mysql-server mysql-client -y
Follow the onscreen instructions for installation BUT when it asks you for what web server you want to configure mysql for, it may seem you only have 2 choices, but, you can skip this entirely by just moving to the “OK” button on the bottom. I made the mistake of hitting “Apache” my first time, and messed up my installation of nginx. Learn from my error. Now we need to install mysql securely by executing the following:
sudo mysql_secure_installation
Enter the password you set for mysql in the previous screens, and then it's off to PHP :-)
sudo apt-get install php5-fpm php5-cli php5-mysql -y
Now, by default, “cli” and “fpm” use separate config files I.e. php.ini. I think it is a good idea for them to both use the same php.ini, don't you? So, execute the following:
cd /etc/php5/cli
sudo mv php.ini php.ini.backup
sudo ln -s ../fpm/php.ini
Once your VPS, Dedicated or home server does its thing with all that computing and such, we need to reconfigure the nginx configuration again.
cd /etc/nginx
sudo nano nginx.conf
Now we need to add a bit of code to this config. Find this area:
http {
    charset utf-8;
NOTE: In PHP, code is started with { and ended with } so in the code above this, that “http {” part will need to end with a ”}”, so the next piece of code I will ask you to insert into the nginx.conf has to be between where the ”{” starts and where it ends with the ”}”. I usually place it at the last possible part where the code ends. Food for thought.

…and add the following to the “http {}” block:

# Upstream to abstract back-end connection(s) for PHP
upstream php {
    server unix:/tmp/php5-fpm.sock;
}
Try to keep the code in line with the rest of the configuration file, as that is good practice, and avoids errors. Now we need to get the default site ready so it can execute PHP pages. It needs to be in the “server {}” block, and the same rules apply as the above addition.
sudo nano /etc/nginx/sites-available/default
Now add this:
# pass the PHP scripts to FPM socket
location ~ \.php$ {
    try_files $uri =404;
 
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
     
    fastcgi_pass php;
 
    fastcgi_index index.php;
 
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT /usr/share/nginx/www;
 
    # send bad requests to 404
    fastcgi_intercept_errors on;
 
    include fastcgi_params;
}
Now we need to go to the “www.conf” file. Execute the following:
cd /etc/php5/fpm/pool.d
sudo nano www.conf
Find this line:
listen = 127.0.0.1:9000
and change it to the following:
listen = /tmp/php5-fpm.sock
Save with CTRL+O, Enter, The CTRL+X to exit. Now we need to restart Nginx and Php.
sudo service nginx restart
sudo service php5-fpm restart
Note: When I did this the first time, I got an error stating that there was a missing ”{” or ”}” at the end of the config. If you get this, just go to whatever file it is complaining about and scroll to the line it is telling you that has the problem, then add or delete whichever is is complaining about. Just about done, young nginx grasshopper. We need to test out our brand new web server with PHP! Nano into your web root and create the following file:
nano /usr/share/nginx/www/index.php
And create this:
<?php
 
phpinfo();
 
?>

Note: Those brackets are the brackets above “9” and “0” not the ones above the “enter” button. It may be hard to see, so just a word of caution.

Hit CTRL+O, Enter, Then CTRL+X and let's see if we have achieved our goal!

Go to your web browser and type in your ip or host (if you set one up with no-ip). If everything is running smoothly you should see a beautiful blue page outlining all your php information! If you get this, get up, prepare yourself, and do a happy dance.

Now! Here is where a dilemma comes in to play. Until a while back, I thought phpmyadmin was the only GUI for mysql. It may be the most widely used, but, there are alternatives, and one in particular caught my eye. It is clean, with an updated look and feel. Very user friendly, and does just about everything that phpmyadmin does, but is much more simple and better to look at. Plus, installation is literally the easiest of any front end I've ever used.

Installing SQL Buddy

So if you want to use sql buddy, download this file , and unzip it. Now FTP into your server and place the folder into your nginx webroot. Thats it! Note: In a live environment (accessible by others) I strongly recommend you rename the SQL Buddy folder to something benign and meaningless to make it harder for others to find it if you got hacked. Name it to something like “uisy”. It won't make any difference to your installation, the only change would be instead of accessing it VIA http://your-server/sqlbuddy it would be http://your-server/uisy

SQL Buddy Interface: i.imgur.com_uwj85s9.jpg

Now that is for SQLbuddy. As you can see from the image above, it looks a lot more nicer compared to phpmyadmin. But, if you are hell-bent on installing phpmyadmin, then follow these steps.

Installing PhpMyAdmin Instead Of SQL Buddy

sudo apt-get install phpmyadmin -y
Now we go to the web root and link it to phpmyadmin.
cd /usr/share/nginx/www
sudo ln -s /usr/share/phpmyadmin
As stated above, you should rename phpmyadmin to avoid potential security threats.

PhpMyAdmin Interface:

On either SqlBuddy or Phpmyadmin, you login with the mysql password you set during the install.

Conclusion

That folks, concludes this tutorial on Nginx, php5-fpm and sqlbuddy/phpmyadmin on Ubuntu 12.04. I hope you have learned something here instead of just copy pasta. I will be doing a lot more of these tutorials, so keep a close eye on my page!

How To | Reference | Programming


QR Code
QR Code how_to_install_nginx_and_php_and_configure_on_a_vps (generated for current page)
 

Advertise with Anonymous Ads