LAMP Installation Guide
Getting the Latest Updates
Before we start installing software, we'll make sure our system is up to date. To install the latest updates and patches for Ubuntu, run these two commands (you'll have to enter your admin password since you will be using sudo):
sudo apt update
sudo apt upgrade
You will be asked to enter the root user password You might also have to enter 'y' to proceed with the command.
THIS WILL TAKE SOME TIME, MAYBE WE SHOULD SHARE A STORY!
Check Your Version of Ubuntu
To check your version of ubuntu
lsb_release -a
Install and Configure Git
sudo apt install git
Once that finishes, you can check to see if it got installed by entering this command:
git --version
After installing Git, you'll want to configure it to know your user name and email address (you can make up whatever user name you want.)
git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "MY_NAME@example.com"
Install Apache2 (Web Server)
Run this command to install the Apache2 Web Server (one of the most widely used web servers on the net):
sudo apt install apache2
To verify that Apache has been installed, launch FireFox (or Chromuim) and enter localhost for the url. You should see the Apache2 Ubuntu Default page, which confirms that your apache server is up and running!
CONGRATULATIONS!!! You have just installed a web server on your Ubuntu vm. Now you can build websites and host them on your web server.
Soon we will replace the default apache page with your own web page.
Setting Permissions on the Doc Root Directory
When you install Apache2 on an ubnutu machine, the doc root directory will be:
/var/www/html
To save yourself some trouble, you should change the permissions of the doc root dir so that you are the owner of the folder. Normally you would have to be very careful about how you set permissions for any file/ folder on a live server. But since this is a vm that we'll be using solely for development (and messing around), the easiest thing to do in order to avoid permission issues is make yourself the owner of the doc root directory.
To change the permissions, run these two commands in your terminal (use YOUR username):
sudo chown yourusernamegoeshere /var/www/html
sudo chmod 755 /var/www/html
The first command makes you the owner of the doc root dir, and the second one sets the group permissions (the 7 allows the owner/you to read write and execute files, the first 5 allows group members to read and execute, the second 5 allows anyone to read and exectute files within the folder - we can talk more about linux permissions in class).
If you run this command you should see that you are the owner of the doc root rolder, and the group assigned to it is 'root'. The root group in linux is similar to the admins group on windows:
ls -al /var/www/html
Apache Log files
When the apache server is running it will log all requests and all errors it encounters. The location of the log files is /var/log/apache2.
Run this command to look at the files in the /var/log/apache2 folder (there's an access log and an error log):
ls -al /var/log/apache2
The access.log file is a listing of every HTTP request that is sent to your web server. And the error.log file will display any errors that the server encounters.
Here's a fun litle command that comes in handy when you are trouble shooting...
Use tail -f to view the a live version of the access log:
tail -f /var/log/apache2/access.log
You'll see a live version the access.log file. The tail command will display any live updates being made to the file. To see this in action, go to the browser and make a request for localhost. If you keep an eye on the terminal window while you do this, you should see the request being added to the access.log file.
To stop the tail command from using your terminal, press Ctrl + c.
Install PHP
Now we'll install PHP. We also need to install a few extra packages go with it.
Run this command to install 3 packages:
sudo apt install php libapache2-mod-php php-mysql
This will install the php package, and two others. The libapache2-mod-php package allows your apache server to interpret php code, and the php-mysql package allows you to write php code that connects to a mysql database.
Now that PHP is installed, restart the Apache server for the changes to take affect (you won't see any output in the terminal after you run the command):
sudo service apache2 restart
The previous command shows the old way of restarting services (the 'service' comand). The 'new' way of restarting a service is this (it uses the 'systemctl' command):
sudo systemctl restart apache2
To test that php is working properly, create a simple .php page and then view it in the browser. We'll be working in the doc root folder of the Apache web server. So let's change to that directory now:
cd /var/www/html
Before we create a php page, we need to remove the default web page in the doc root folder (this page was created when we installed Apache):
sudo rm index.html
Now let's create an index.php page in the doc root of the web server:
sudo touch index.php
Next let's edit the file using the Nano editor:
sudo nano index.php
Enter this php code into the index.php page (be careful to copy it exactly, to avoid any errors when we run the page)
<?php
echo("Hello World!");
?>
To save the file, press ctrl+x and then press y. Then press the Enter key.
Now test the page by opening your browser and entering localhost in the URL bar. You should see the text 'Hello World!'
Install MySQL Server
To install MySQL Server on your ubuntu VM, run this command:
sudo apt install mysql-server
Next we need to set the password for the root account in MySQL. Note that this is not the same as the root account for your Ubuntu OS, this is the root account for the database server (this is a little confusing!).
Log into the mysql server (as sudo) by entering this command:
sudo mysql
In the terminal, you should see that you are now working on the mysql server because your command prompt looks like this: mysql>. You are running MySQL as the root user of the operating system, which has complete control over everything in MySQL (and everything else on the system).
Now we'll update the root account for MySQL, we'll use this account to log into the server from our PHP code, as you'll see later.
Enter this command to set a password for the root user of the mysql server (PAY ATTENTION TO THE QUOTATION MARKS):
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';
Hopefully you'll see some output like 'Query OK'.
Now run this command to make the previous change take affect:
FLUSH PRIVILEGES;
Now you can exit mysql server by entering this command
exit;
Let's see what it's like to log into the MySQL server from the command line (it's not pretty!) To log into MySQL server:
mysql -u root -p
This time we are not running the mysql command as sudo, but we are using the -u switch to specify that we'll be logging in as the root MySQL account, which we previously set to have a password of 'test'.
You will prompted to enter the root MySQL account, enter test.
Note that the command line is now using the mysql> prompt, which indicates that you are working in the mysql server.
To see what databases are set up on the server:
show databases;
When you enter a command in mysql, you must end it with a semi-colon.
To switch to the mysql database (which has table that allow you to configure the mysql server), run this command:
use mysql;
To show the tables in the current database, run this command:
show tables;
To show the columns in the users table:
describe user;
To select all rows from the user table:
select * from user;
To exit mysql:
exit;
Securing Your MySQL Installation
A fresh installation of MySQL Server will have some default settings that are not secure. So run this next command to change some of the default settings for MySQL (Note that if you chose to install MariaDB the questions are a little different):
sudo mysql_secure_installation
When this program runs, you'll be prompted to answer the following:
- Enter the password for user root: test
- Use validate password plugin 'VALIDATE PASSWORD...' - No
- Change the password for root - No
- Remove anonymous users - Y
- Disallow root login remotely - Y
- Remove test database and access to it - Y
- Reload privilege tables now - Y
Congratulations, you have just set up a LAMP server!!
Now we need to make some configuration changes and install a few more tools that we'll be using for development.
Dealing with PHP errors
The default installation of PHP will not display error messages in the browser (we'll change that later). This means that if you have an error in your PHP code, when you view the page in the browser you'll get a blank screen (no error message).
This makes it very hard to debug your code!
But you can use the tail command to view the error log.
First, let's put an error in our code. Use nano to update the index.php file that you created earlier We'll put an error in the code, so that we can see how errors are handled on or server. Update the index.php file to like so (it is attempting to echo a variable ($x) that has not been declared, this will cause the error):
<?php
echo($x);
?>
Make sure to save your changes.
Then run this command in the terminal: (it will 'watch' the error log file and display changes to it in real time)
tail -f /var/log/apache2/error.log
Now open your homepage in the browser, you won't see an error message in the browser (instead it should be a blank screen, or maybe a screen that indicates an error has ocurred. But you should see a new error in the terminal (the error was just written to the error.log file)
In the terminal window, press Ctrl+c to stop watching the error log file.
Changing the PHP configuration file to display errors
The final step in configuring PHP is to make it display errors in the browser so that we can easily see them in the browser when they occur (looking in error.log can be tedious).
This step is a little hairy. You may skip it and move on to the instructions on Installing MySQL, but if you do, please ask me about doing this step together in class. It will save you a lot of time if you can see errors in the browser, rather than looking through the log file.
Usually when you install Apache and PHP, error messages are hidden by default because live web servers should never display them.
But as developers, we need to be able to see error messages. You can do this by making an update to a php configuration file (Linux is famous for it's configuration files).
The main configuration file in PHP is named php.ini, and it's located in the /etc/php/8.1 folder.
We are about to update the php.ini file so that error messages are displayed in the browser (they will also continue to be logged in the error.log file).
NOTE: When you are about to change a configuration file in Linux, it's a good idea to make a copy beforehand, just in case you make a mistake in editing the file.
First cd into the /etc/php/8.1/apache2 folder:
cd /etc/php/8.1/apache2
Now run this command to make a copy of the php.ini file:
sudo cp php.ini php.ini.backup
This creates a copy of php.ini named php.ini.backup.
Now that we have a backup, run the nano command an open the php.ini file in the terminal:
sudo nano php.ini
The setting that we need to change is called display_errors. The php.ini file is rather large, so we can search for this setting by pressing ctrl + w and then typing 'display_errors' (then press the enter key).
Here's a twist - the first instance of 'display_errors' is commented out (comments in the php.ini file start with a semi colon, which is something that I find strange). We need to find the next occurrence, so press ctrl + w one more time and then press enter (you DON'T need to re-type 'display_errors').
Once you find this setting, change it from Off to On Press ctrl + x to exit nano, and then type y to save your changes (then press the enter key).
Now that PHP has been re-configured, we need to restart the Apache server for the changes to take place:
sudo systemctl restart apache2
Any time you change a configuration file for a server, you'll have to restart the server for the change to take affect.
To see if the configuration change worked, reload localhost in the browser and instead of a blank screen, you should see the error message on display. It will say something like: Warning: Undefined variable $x in /var/www/html/index.php on line 2.
Install PHPMyAdmin
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
- You'll be prompted to choose your web server, apache2 is select by default so just press 'enter'
- You'll asked to use dbconfig-common, yes is selected so just press 'enter'
- You'll have to enter a password and confirm it. enter test.
Then restart the apache server
sudo systemctl restart apache2
Now we need to configure Apache to enable the PHPMyAdmin website Warning - in this next step you will work with two files that have ALMOST the same name (although they are in different folders). So pay attention!
First we have to edit an Apache configuration file called apache2.conf. But before we edit this file, let's make a backup in case we make a mistake when editing.
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backup
Now that it's backed up, go ahead and open this file in the Nano editor:
sudo nano /etc/apache2/apache2.conf
Use the down arrow key to scroll to the bottom of the file and add this line at the end of the file:
Include /etc/phpmyadmin/apache.conf
This line will tell the Apache server to look at the file /etc/phpmyadmin/apache.conf for additional configuration changes (it's including the configuration settings in the /etc/phpmyadmin/apache.conf file). This file was added when we ran the install command for phpmyadmin earlier.
Press ctrl+x and then y (and enter) to save the changes.
Now restart the Apache server, so the configuration changes take affect.
sudo systemctl restart apache2
To confirm that the PHPMyAdmin website is properly installed on your Apache Server, open a browser and navigate to this URL: localhost/phpmyadmin
You should be prompted to log in. Enter root and the password that you created when you set up the MYSQL server (test).
Install VS Code (you may have already done this)
Open the Application Manager (click on the A icon in the favorites bar) and click on the search icon (the magnifying glass)
Enter Code (I don't know why it's just called Code instead of VSCode), then click on the Code icon to start the install.
Install Chromium (you may have already done this)
Use the Application Manager to install Chromium.
Install XDebug (a php plugin that will allow us to debug our PHP code)
Enter this command into the terminal:
sudo apt install php-xdebug
Update the php.ini file:
sudo nano /etc/php/8.1/apache2/php.ini
Scroll down to the end of the file, you could hold the down arrow key for this, but it's faster to use the 'Page Down' button on your keyboard.
Add these lines to the end of the php.ini file:
zend_extension=xdebug
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9003
xdebug.start_with_request = yes
xdebug.mode=debug,develop
Then save the file (press ctrl + x then y, then the enter key).
Restart the apache server:
sudo systemctl restart apache2
Set up the debugger for PHP
- Open VSCode and then choose File then Open Folder... then open your doc root directory (/var/www/html). When the Ubuntu File Explorer opens, you may have to choose 'Other locations' to navigate into the var directory.
- Click on the Extensions icon in VS Code and install the PHP Debug extension (if there is more than one to choose from, pick the one that says Xdebug).
- Click on the Debug icon in VSCode
- Click the link to create a launch.json file
- Choose PHP from the drop down that appears.
- This will create and open a file named launch.json.
Now you should be able to use the debugger. To try it out, follow these steps:
- Click on the files icon in VS Code to see the files in the doc root dir (which now is just index.php)
- Click on the index.php file so that you can see the code for it in the editor window.
- To set a break point on the line that calls the echo() function, right-click on the gutter next to the line number for the line and choose 'Add breakpoint'
- Now that you have set a breakpoint, you can run the debugger by clicking the Debug icon on the left.
- Near the top, you should see a drop down appear that says Listen for Xdebug. If the drop down is set to something else, then change it to Listen for Xdebug.
- Press the little green play icon/button next to the drop down, this will start the debugger.
- To hit the break point, you will have to load the page in the browser (go to localhost, since we are debugging the home page for the server).
- You should get redirect back to VS, with the debugger stopped in your break point. You can now step through the code line by line (or step over code) by pressing the buttons that appear at the top of VS Code.
- To stop the debugger, press the stop button (the red square box)
Enable the ModRewrite module (plugin) on your Apache server
ModRewrite allows your Apache web server to perform url rewriting, which is an extremely powerful tool. We'll be using it heavily later in the course.
To activate this module, open a command line terminal and enter this command:
sudo a2enmod rewrite
You may be prompted to restart the apache server, to do so enter this command:
sudo systemctl restart apache2
Now that you have enabled the rewrite module, you can do something very cool and powerful, which is called URL rewriting. But we need to make another configuration change before we can actually do it.
We need to update the Apache configuration file for our server's default website. The change that we need to make is to allow what are called .htaccess files to be used for the site. .htaccess files allow us to do all kinds of things for a site that runs on an Apache server. We'll be using it to configure URL rewriting in a moment.
To allow the use of .htaccess files we need to update Apache configuration file for our default site (you can configure a single apache server to run more than one website). The config file for the default site is /etc/apache2/sites-available/000-default.conf
Let's make a copy of the file so we have a back up:
cd /etc/apache2/sites-available/
sudo cp 000-default.conf 000-default.conf.backup
Now we can edit the file, let's use nano for that:
sudo nano 000-default.conf
Add the following text inside the VirtualHost element, I like to add these file just before the line that has the closing tag for the VirtualHost element. Make sure the element we add is indented within the VirtualHost element (indentation may matter):
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
To exit nano press ctrl + x and then press y and then Enter to save your changes. Then restart your Apache server (you may see a message telling you to restart the web server using a different command than the one below, but either works):
sudo systemctl restart apache2
Next create a folder called url-rewriting-test, in the doc root directory of your web server.
mkdir /var/www/html/url-rewriting-test
Put an .htaccess file in the folder, we'll talk about this file in class (remind me!):
cd /var/www/html/url-rewriting-test
touch .htaccess
nano .htaccess
Add this to the .htaccess file:
# Turn on the mod_rewrite plugin
RewriteEngine on
#If the file being requested exists, then do not redirect:
RewriteCond %{REQUEST_FILENAME} !-f
#If the directory being requested exists, then do not redirect:
RewriteCond %{REQUEST_FILENAME} !-d
#Redirect all requests for this folder (and children)
#to index.php and pass the requested resource in the query string
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
Put an index.php in the url-rewriting-test folder,
touch index.php
nano index.php
Add this content to index.php:
<?php
$page = isset($_GET['page']) ? $_GET['page'] : "";
echo("The page you requested: <b>{$page}</b><br>");
echo("The php script that is running: <b>{$_SERVER['SCRIPT_FILENAME']}</b><br>");
die();
?>
Finally, use your browser to navigate to localhost/url-rewriting-test/blah.html.
Install a Modern Version of NodeJS (NVM)
Run the following commands to install Node Version Manager, which allows you to have multiple versions of NodeJS installed (and you can easily switch between them).
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.bashrc
nvm install node
TAKE A SNAPSHOT OF YOUR VM HERE (In case things get messy, you can role it back to this state)
Questions:
- What's the difference between these two commands:
apt update
apt upgrade
- Why do include 'sudo' when you run certain commands?
- What is Git?
- What is the difference between Git and GitHub?
- What is an Apache server? What role do Apache servers play on the world wide web?
- What did you do to confirm that the Apache web server was up and running?
- What do these commands do: chown and chmod
- What does the tail command do, and what happens when you add the -f option?
- What additional packages did we install when we installed PHP,
- What is the path to the doc root directory for the Apache server that you installed?
- When should you restart the Apacher server?
- What command would you run to restart the Apacher server?
- What command did we use in order to make a back up of the main php configuration file?
- What is the name of, and path to, the main configuration file for PHP?
- What is the name of, and path to, the back up that we made of the main configuration file for PHP?
- What change did you make to the PHP configuration file?
- Why did we make the change?
- Why do you think that the default installation of PHP does not show error messages in the browser?
- What is URL rewriting, and what module did we install in Apache to allow us to do it?