How to setup a light Raspberry Pi webserver!

Hey Guys!
Today I'm going to show how to make a raspberry pi webserver.

If you are a person who uses a raspberry pi for multiple reasons (like me :D) having a lightweight (software-wise) setup is essential because a webserver could slow it down.
The web server that I'm going to install is Nginx because it is lightweight. Also, I'll install MySQL for handling databases.

First, run the following commands:
sudo apt-get update 
sudo apt-get upgrade
This will update the software lists and install software updates. Next, execute:
sudo apt-get install nginx mysql-server php5-fpm php5-mysql
This will install nginx (the server), mysql (the database), PHP 5 and the PHP MySQL plugin.
When installing MySQL, it will ask you to enter a password for the root user. After that, wait for it to finish. Once done, restart php5-fpm with:
sudo service php5-fpm restart
Next,  edit the configuration file for the default webpage:
sudo nano /etc/nginx/sites-available/default
Change the text after the line which starts with root to "/var/www;" (no quotes). This says the web server to look for files in /var/www (create the directory if it doesn't exist)

Uncomment (remove the # symbol) the lines under #PHP settings. You should be left with:
location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
Once that's done, exit nano (Ctrl-X, Y, Enter) and restart nginx:
sudo service nginx restart
If you didn't make the /var/www folder, create it and set correct permissions by:
sudo mkdir /var/www 
sudo chown -R /var/www www-data:www-data 
Now, make a test PHP file:
sudo nano /var/www/index.php
And type the following,
<html> 
    <body> 
        <h1>It Works!</h1> 
        <?php phpinfo() ?> 
    </body> 
</html>
Once done, exit nano and open the following URL in your browser:
http://<your rPi's IP address>/
If you see "It Works!" and a table, your web server's successfully set up.

Now, for the database.
MySQL will run as-is but it is quite heavy. To optimize it, run the following commands:
sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.bak 
sudo cp /usr/share/doc/mysql-server-5.5/examples/my-small.cnf /etc/mysql/my.cnf
This will load the configuration file for small systems. Restart MySQL using:
sudo service mysql restart
Reload your browser which has your rPi's test web page. If the MySQL extension works, it will be shown in phpinfo().

Congratulations on your rPi web server!
Thanks for reading!
-Technohacker

Comments

Popular Posts