Server setup: Apache, MySQL, PHP
In this, you will complete the installation of the last three components of the LAMP stack:
- Installing the Apache web server including configuring CGI
- Installing the MySQL relational database
- Installing PHP server-side scripting including MySQL libraries
Part 1: Installing Apache.
In the last assignment, you setup and secured your VM.
In this assignment you will be creating your own server for use during the course.
Your web server will be built using the LAMP
software stack consisting of Linux (operating system), Apache (web server), MySQL (database), and PHP (server-side scripting).
Install the Apache web server using Ubuntu's package management software apt-get:
- sudo apt-get install apache2
Try and connect to your web server using a browser by giving the url http:// followed by the IP address of your server.
You'll find the browser will try for awhile and then return an error message.
HTTP operates by default on port 80.
Currently this port is blocked by the firewall you configured in the last assignment.
Open up port 80 using the following command:
Now test out the web server:
- Open a browser to the IP address of your server. You should see Apache's default "It works!" web page.
- Practice starting/stopping/restarting your web server using: sudo /etc/init.d/apache2
- Test making a change to the default main web site page located at: /var/www/html/index.html
- The owner of index.html is root, so you'll need to sudo in order to modify the file.
Optional: By default, apache runs the web server as the username www-data and the group www-data.
You can add yourself to this group and then modify the permissions of the directory and any current files:
- sudo gpasswd -a "$USER" www-data
- sudo chown -R "$USER":www-data /var/www
- chmod 2770 /var/www/html
- chmod 0660 /var/www/html/*
Familiarize yourself with Apache's logging:
- Log files live in /var/log/apache2
- The log directories are owned by root, so you'll need to sudo to list or view files in this directory.
Part 2: Setting up CGI.
You need to enable CGI (Common Gateway Interface) as it is not enabled by default in Apache.
Part 3: Install MySQL.
MySQL is a popular open source relational database.
You will eventually be using it in some of your web applications developed for the course.
Here are the installation steps:
- First you will install the MySQL database as well as libraries that will allow access via PHP pages hosted the Apache server:
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
- During the installation you will be asked to set a root password. Use the same password you set for my user account on your server
- Once the installation completes, activate it with: sudo mysql_install_db
- Finish by running this script which helps lock down the security: sudo /usr/bin/mysql_secure_installation
- You can answer No to changing the root password. Answer Yes to all the other options.
Part 4: Install PHP.
In various assignments, we will be using PHP server-side scripting. This is available as an optional Apache module. Install by following the following steps:
- Install PHP and the Apache PHP module: sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
- Answer Yes to the prompt twice.
-
The Apache web server has a standard list of filenames that it looks for when a visitor goes to a directory on your server.
If you would like to default to serving index.php, move it to the front of this list of the DirectoryIndex option in the file located at /etc/apache2/mods-enabled/dir.conf
-
Create a test page in the root of your web server named info.php.
Add the line: <?php phpinfo(); ?> to the file.
- Test that you get the informative PHP info page by going to http://X.X.X.X/info.php
Part 5: Hello MySQL!
- Create a simple test database and display results from it from a PHP page.
- Log into your MySQL server as root:
mysql -u root -p
- Create a test database, make it the active database:
create database namevalues;
use namevalues;
- Create a table to hold key/value pairs:
CREATE TABLE data
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
value VARCHAR(255)
);
Populate the table with some example name/value pairs, for example:
INSERT INTO data (name, value) VALUES ("hello", "world");
Test out a query against your new table:
SELECT * from data;
For security, it is best not to use the root user to access your database from PHP.
Instead, you will create a user that only has access to this specific database.
In the following example, you change 'mysecret' to a more secure password of your own choosing.
CREATE USER 'web_namevalues'@'localhost' IDENTIFIED BY 'mysecret';
GRANT ALL PRIVILEGES ON namevalues.* TO 'web_namevalues'@'localhost';
FLUSH PRIVILEGES;
Create a PHP page in your web server root directory called name_values.php that does the same query, displaying the results in a nicely formatted table in valid HTML (the below program just spits out some ugly text).
Here is a skeleton page that makes all the MySQL specific calls and shows all the name/value pairs in the database:
<?php
$mysqli = mysqli_connect("localhost", "web_namevalues", "mysecret", "namevalues");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM data";
$res = mysqli_query($mysqli, $sql);
if ($res)
{
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC))
{
$name = $newArray['name'];
$value = $newArray['value'];
printf("Name '%s', value '%s'<br \>\n", $name, $value);
}
mysqli_free_result($res);
}
mysqli_close($mysqli);
?>
You are now the proud owner of a full-fledged web application server based on the LAMP stack, exciting times!
Submission and grading.
There is nothing to turn in for this assignment. But I will be testing you completed the lab by doing the following:
- Logging into your server, starting Apache, testing logging in to the MySQL root user
- Testing static content is served by going to http://X.X.X.X/index.html
- Testing CGI works by going to http://X.X.X.X/cgi-bin/hello.sh
- Testing PHP works by going to http://X.X.X.X/info.php
- Testing your database test page: http://X.X.X.X/name_values.php
Page last updated: January 26, 2015