Saturday, February 21, 2015

How to install Apache, PHP, MySQL and Bluefish on Linux Mint.



by Joseph Roten
Saturday, February 21, 2015


Would you like to learn Apache, PHP, or MySQL, but don't want to invest in a web server?
Would you like to learn html?
Would you like a very friendly development environment for creating websites?

If you answered YES to any of these questions, then read on; this blog entry is for you.

In this blog entry, I will be explaining how to install Apache, PHP, and MySQL on your Linux Mint computer (workstation or laptop), how to set it up, and get you started on the road to using it as a website development platform and learning tool.

So, what exactly are Apache, PHP, and MySQL?
Here is a very short, and very simplified answer to the question.

Apache is a software package that allows an Internet server to 'serve up' web pages. When you use your web browser on your computer to access a web site, it is an Apache server some where on the Internet that sends the text and images to your computer.

PHP is a scripting language that runs on top of Apache. It allows you to add 'logic' to your web page. If a web page does anything other than just shows text and pictures, chances are good that it's PHP doing the work.

MySQL is a database package. If you buy something on-line, looks up a street address, sign up as a new user on a website, or so forth, it's most likely that this information is being stored in a MySQL database.

These 3 packages combined, plus Linux, create what is commonly called a LAMP server; Linux, Apache, MySQL, Php. LAMP servers are the heart and sole of the Internet. The vast majority of Internet web sites and services run on such servers.

This blog entry describes how to install Apache, PHP, and MySQL on your Linux Mint laptop/workstation. Basically, creating your own private LAMP server. Since it's YOUR server, you are the admin and user, and you can do pretty much what you please with it.

Please note that we are NOT taking anything away from the way your computer works. You will still be able to use it to surf the Internet, do email, write documents, and anything else that you are currently using your computer for. We will simply be adding some really cool abilities to your computer.

This blog entry will also describe how to install a package called Bluefish. For those of you who might not have ever heard if it, Bluefish is a very simply text editor that makes writing html, PHP and Javascript code, very easy. It has some very nice functions that makes developing web pages very simple and fast, and it's very easy to learn.

In order to do the install described in this blog post, you will need a working Internet connection. After everything has been installed and configured, you can then use your LAMP server without having to be connected to the Internet. Since both the Server and Client (web browser) will be running on the same physical hardware, you won't need the Internet connection to use it.

IMPORTANT: You will also need a pen and paper to take notes and record passwords. If you don't have such, this is a good point to stop and get it.


Step 1. Installing the software

To do the install, open a terminal window (click on 'Menu' at the bottom of the screen, then on 'Terminal'), and enter each of these commands one at a time. You will need to wait for each command to complete before entering the next. You will be prompted to create a new password for the MySQL root account, and the PHP Admin account. Be sure to record these on your paper and keep this info in a safe place. This install processes should take you about 10 minutes depending on the speed of your Internet connection.


sudo apt-get update
< enter your system password when prompted >
sudo apt-get install apache2 -y
sudo apt-get install php5 libapache2-mod-php5 -y
sudo /etc/init.d/apache2 restart
sudo apt-get install mysql-server -y
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin -y
sudo /etc/init.d/apache2 restart
sudo apt-get install bluefish -y
sudo chmod -R 777 /var/www
echo "<?php phpinfo(); ?>" > /var/www/html/testphp.php


Step 2. Setting up your MySQL server

IMPORTANT: For safety reasons, you should only use the MySQL root account for creating new databases and users. Never use your root account for daily tasks, it's just too easy to accidentally do something damaging.

So, our next step is to use the MySQL root account to create a new database called 'sandbox', a new user called 'user1', and grant user 'user1' full rights to database 'sandbox'. After this is done, you can then safely use the user1 account to learn and work with MySQL.

Using the same terminal window you used to install the software, enter this command:

mysql -u root -p

Enter your MySQL root password when prompted. This is the password you created when you installed the MySQL server.

You should now see a prompt that looks like this:

MySQL>

If you see this prompt, you are successfully logged into your MySQL server as 'root'.
Now, cut and past this text into your terminal window. Note that MySQL commands always end with a semi-coluon. The 'GRANT ALL' command shown bellow is one long command on several lines. It's not until it sees the ';' that it actually does anything.


CREATE DATABASE sandbox;
USE sandbox;
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'xxx123';
GRANT ALL ON sandbox.* TO 'user1'@'localhost'
    WITH MAX_QUERIES_PER_HOUR 320
    MAX_UPDATES_PER_HOUR 310
    MAX_CONNECTIONS_PER_HOUR 350
    MAX_USER_CONNECTIONS 6;
SET max_error_count = 65530;
FLUSH PRIVILEGES;


Next, you will want to set up the password for the user1 account. Enter this command at the MySQL> prompt, replacing the word 'yourpassword' with the password you want for this account. Be sure to also record this password on your paper so you won't forget it.

SET PASSWORD FOR 'user1'@'localhost' = PASSWORD('yourpassword');

And finally, enter the word 'EXIT' to sign out of the MySQL root account.


Step 3. Testing everything.

We are now ready to test.
Using your web browser on your computer (Chrome, FireFox, Opera, or other) bring up each of these web pages. Note that these pages are ON YOUR OWN HARDDRIVE, and not on the Internet. Your web browser will see your computer as if it was an Internet web host server, by the name of 'localhost'.
Pertly cool!
If you get a 404 error on any of these, something went wrong with the install, and you should try again.



To test the Bluefish editor, enter this command into the terminal window. You should see the contents of the index.html file. To leave Bluefish, just do a file/quit like any other application.

bluefish /var/www/html/index.html


To test MySQL, enter the following commands in the terminal window.

mysql -u user1 -p
< enter your user1 password when prompted >
use sandbox;
status;
exit


Step 4 (optional). Installing Wordpress

If you are a Wordpress user, or would like to learn Wordpress, now would be a good time to install it as well. There is an extremely good article on how to do this at http://community.linuxmint.com/tutorial/view/828.



Step 5. A few things you should know.

You can create new html or PHP documents (web pages) and save them into the folder /var/www/html    Any files saved into this folder will be visible to your web browser as http:\\localhost\filename
For example: If you create a file called /var/www/html/aboutme.html you will be able to see it from your web browser as http:\\localhost\aboutme.html

Your web browser will be able to access files in your /var/www/html folder even if your computer does not have an active Internet connection. So you can use the Bluefish editor to create new html or php documents, any ware, any time, and test them, regardless if you have an Internet connection or not.

The Bluefish editor can be used to create or change html or PHP files. Just remember to save them into the folder /var/www/html so that your browser can see them. It's a fairly simple and straight forward editor, and most users will be able to use it right from the start. But if you feel that you really need to see the documentation, it can be found at: http://bfwiki.tellefsen.net/index.php/Manual_2_ToC

Most Internet web hosting providers (like FatCow, HostGator, and others) have a function in their file manager that will allow you to upload your html and PHP files from your computer to your web account. So after you have your web page looking the way you want it on your computer, you can then move it into 'production' so that the whole world can see it.

You might want to think about creating a new folder in your home folder called something like 'notebook', and downloading any manuals and documentation about web page development to that folder. That way, you can have all your documentation and notes available even if your laptop does not have an Internet connection. A few documents you might want to consider are in the links at the bottom of this blog entry.

IMPORTATN..IMPORTATN..IMPORTATN:
Always remember to make backup copies of ANYTHING you write (including notes, scripts, and web pages), to an external hard drive or USB thumb drive. I suggest you make this a daily 'end of the day' ritual.

Always remember that your hard drive, especially in a laptop that you carry around with you and is subject to bumps and vibrations, could fail AT ANY TIME!!

You should keep your backup external hard drive or USB thumb drive in a safe place, like the top shelf of a closet or behind a book on your book shelf. 

Please DON'T carry your backup device around with you in a backpack. If you feel that you really need to carry your backup device with you, I suggest you have a second, or maybe even a third backup device for that purpose. Thumb drives don't cost that much, and you can NEVER have too many backup copies.



A few words about PHP and Javascript:
Javascript, like PHP, is a scripting language that allows your web pages to do stuff other than just display text and images. A great many web pages use Javascript to display side shows, videos, run user login scripts, games, and a great many others things.

You may have notices that I have not said anything about installing Javascript yet. The reason why is...well...there is nothing that you need to install. The Bluefish editor will allow you to write your scripts, and everything you need to run them is already installed.

A script written in PHP runs (executes) on the the LAMP server, and only passes the output to the client computer. If your security is set up right, the end user CAN'T see your PHP script. So any IDs, passwords, or database scripts inside your PHP are secure (the user can't see them). This makes PHP an excellent language for doing anything for a business, working with money, or working with a user's personal information.

A script written in Javascript is first downloaded from the LAMP server to the client computer, and then runs (executes) on the client computer. A savy computer user will be able to view your Javascript, along with any IDs and passwords you may have in it. This makes Javascript very un-secure as compared to PHP. However, Javascript tends to be much faster than PHP, and has many more capabilities. Javascript is an excellent choice if speed, and NOT security is what you are looking for.

So...which one to use for your website?
BOTH!
There is no reason why a web page can't use a mix of PHP and Javascript. In fact many do just that. So...I would recommend that the student learn, and use, BOTH PHP and Javascript.


Step 6. Where to go from here.

Now that you have a development environment installed and running, it's time to start learning and doing. I recommend the following websites to learn more. I will also be writing additional blog posts on these subjects.

HTML5 Tutorials at W3Schools:

PHP5 Tutorials at W3Schools:

Javascript Tutorial at W3Schools:

Using and configuring the Apache web server (YouTube video):

MySQL Tutorials:

Introduction to Bluefish by Jon Morin:

How To Build a Website: HTML, CSS and HTML Editors, By Michael Rohde

Debian GNU/Linux installation notes for PHP

Interpreted Languages: JavaScript, PHP, Python, Ruby (Sheet One)

The PHP Security Manual

The PHP Security Cheat Sheet



That's it for this time. So long for now, and good luck.
Joe.

Last updated 02/21/2015.