6313 views 0 comments

Project #2: Create your own LAMP web server | Raspberry Pi 2

by on December 10, 2015
 

So, it’s been a while since I posted some Raspberry Pi stuff. Been a bit busy so excuse me here. Today we will create a LAMP server on our Pi and host wordpress on it so you can create a kickass blog locally and then if you wish to, you can either buy a dedicated hosting and host it online or maybe you can use your Pi as the server and do a port forwarding to a static IP address so as you open up your Intranet to the Internet to access the blog from anywhere you please. Isn’t that badass? 😉

I am going to make this tutorial/walkthrough as easy as a Pie so you can have it up and running in absolutely no time.

What is LAMP?

If you are not nerdy, it’s probably not the LAMP you are thinking it is. 😉

LAMP is an acronym that stands for LinuxApacheMySQLPHP/Python/Perl which a popular open source web platform used to run a Dynamic HTML web sites and servers. It is the most commonly used Stack by most of the Web 2.0 companies as it is considered as the platform of choice for development and deployment of high performance web applications which require a solid and reliable foundation.

Let’s begin!

As usual, I expect you to have a fully setup Raspberry Pi. If not, visit my wiki thread and follow it from there. Make sure you install Raspbian (the official distro) and you are able to run your Pi headless via SSH. We will be doing all the operations over the terminal.

Once done with the prerequisites above, SSH into your Pi using hostname.local or IP address and start with the flow.

#1. Updates!

Updating software repositories LAMP web server | Raspberry Pi 2

First things first!

Run the following commands one by one to ensure your OS and software repositories are up to date.
sudo apt-get update
sudo apt-get upgrade

You will be prompted if you would like to continue, type y for yes and hit enter to continue. This process may take a few minutes.

#2. Install Apache:

Apache is the one of the most widely used web server applications. It’s the thing that takes the HTML files on your RPi and “serves” them over HTTP . To install Apache, execute:
sudo apt-get install apache2 -y
-y is to auto-confirm the installation.

Restart Apache with the following command:
sudo service apache2 restart

Apache installation on LAMP web server | Raspberry Pi 2

Apache is up!

Apache automatically puts a test HTML file in the /var/www directory called index.html. If you want to look at it, run the following command.

cat /var/www/index.html

Apache2 index html file contents LAMP web server | Raspberry Pi 2

Sample html file contents

To see this HTML in action, go to your web browser of choice and type the Raspberry Pi’s IP address into the URL bar. To get the IP address, execute the command
hostname -I

Apache 2 works LAMP web server | Raspberry Pi 2

It does!

In your web browser, you should see a plain website that says, “It Works!” and two more lines of text. Awesome?!

Pro-tip:

You can go ahead and modify HTML and experiment with the index.html. To do so, execute: sudo vi /var/www/index.html to open the file in text editor. Make some changes in the text, Press Ctrl-X to exit, y to save, and Enter to finalize. Now go back to your web browser and refresh the page. You should see your updated text!

Congratulations, you now have a basic server that can serve HTML to any other computer on the same network. You can stop here if you just want to host some HTML pages, but if you want to setup a full fledged blog, proceed to learn WordPress installation.

#3. Install PHP:

Now that we have a server that can serve HTML, we want to make it dynamic and exciting. That’s where PHP comes in. PHP is additional code that runs every time someone requests the webpage from the server.

WordPress is built upon PHP which allows it to build beautiful dynamic websites and blogs.

PHP also requires a library to work with Apache. We can install them both at the same time by using the following command:
sudo apt-get install php5 libapache2-mod-php5 -y

Like in the last step, let try some things out. As a quick example, let’s add a clock to our webpage!

First remove the index.html file.
cd /var/www
sudo rm index.html

Next, create an index.php file.
sudo nano index.php

Add the following code to index.php:

Now exit, save, and refresh you browser. You should see the date and time update accordingly every time you hit refresh!

#4. Install MySQL

Wordpress also requires a database engine to keep track of all of its data. MySQL is one of the most popular OSS for servers. Like PHP, MySQL also requires a library to work. This time, the library is for PHP. Enter the following command to get MySQL and the required library.

sudo apt-get install mysql-server php5-mysql -y

Installing MySQL LAMP web server | Raspberry Pi 2

Long live The MySQL!

MySQL will prompt you to create a root password. This is different from your login password and different from the RPi’s root password. Enter whatever you like but don’t forget it!

#5. Press the Word!

Now we can finally download and install WordPress. This time however, we have to download it from the WordPress website, meaning we can’t use apt-get. Instead, we have to use wget, which let’s us download stuff from a specified web address. WordPress always has it’s most recent version available at wordpress.org/latest.tar.gz.

First, navigate to the directory that we want to download WordPress to.
cd /var/www
Now we want to completely empty this directory so that WordPress doesn’t run into any problems. This means that you should move any HTML or PHP files that you may have made if you tried any of the tutorials I mentioned earlier. If there’s anything you want to keep, move it to your home directory by entering the following command.
sudo mv * ~/

Now remove any leftovers from the directory.
sudo rm *

While we’re here, we might as well change the owner of the directory to pi so we don’t have to keep entering sudo before every command. Enter the following command.
sudo chown pi: .

We use a period to indicate the current directory(/var/www). This command changes the owner of the current directory from root to pi.

Now download and extract WordPress by using the following command(s):
wget http://wordpress.org/latest.tar.gz

Download wordpress on LAMP web server | Raspberry Pi 2

Fetching latest tar for WordPress

tar xzf latest.tar.gz

Extracting wordpress package in www directory LAMP web server | Raspberry Pi 2

Let’s extract and press!

We now have a latest.tar.gz file and a wordpress folder in our /var/www directory. For everything to work, we need to move everything out of the wordpress folder and into the /var/www directory. After that, we want to cleanup by getting rid of the empty wordpress folder and latest.tar.gz. Do this with the following commands.
mv wordpress/* .
rm -rf latest.tar.gz wordpress

www directory structure for wordpress on Raspberry Pi 2

Cleaned up directory.

The -rf options we used with rm stand for recursive and force. We use recursive to get rid of entire directories and force to get rid of everything without any prompts.

Almost done folks!

#5.1. Link WordPress and MySQL

Creating a wordpress database in mysql on raspberry pi

Databases!

Now that we have WordPress and MySQL, we need to create a database for WordPress. First, connect to the MySQL monitor.
mysql -uroot -p

You will then be prompted to enter the password you set earlier. This will bring you to a MySQL prompt. From here, you can create a WordPress database by entering the following command.
CREATE DATABASE wordpress;

Make sure not to forget the semicolon at the end of the command. After entering the command, you should see the following:
Query OK, 1 row affected (0.00 sec)

Then press Ctrl-D to exit the MySQL monitor.

#5.2. Configure WordPress

For the next step we have to go back to the web browser. Enter your RPi’s IP address again or refresh the page if you’re still there and you should see a list of files and directories that are all clickable. Click on the folder:
wp-admin/

Configuring WordPress on Raspberry Pi LAMP

Let’s begin

This should bring you to a setup page with some instructions on what to do and a button at the bottom that says, “Let’s go!” Click on the button and fill in the information on the next page.

Configuring WordPress on Raspberry Pi LAMP

Fill these details..

Fill out this information and click submit. On the next page you will be presented with a dialog box filled with PHP code. Copy all of the code and create a file called wp-config.php in the /var/www directory of your RPi. Paste the code into this file.
sudo nano /var/www/wp-config.php

Now exit and save wp-config.php and return to your browser.

Click the “Run the install” button at the bottom.

Follow the on screen instructions on the next page to finalize the setup. When you’re done, hit the “Install WordPress” button and it will prompt you to log in. Just login at  with the chosen credentials and, voila, you have yourself a WordPress website accessible from anywhere on your network!

#6. PROFIT!


Now you have your own awesome WordPress blog make it ultra badass. Once satisfied, learn how you can make your website accessible from anywhere in the world. You can buy a domain and set it up with a hosting or you can set up port forwarding on your router, give your RPi a static IP, and set up a dynamic DNS service. I plan to have a guide for this sometime in the near future. Stay tuned! 😎

Be the first to comment!
 
Leave a reply »

 

Leave a Response