February 24, 2015

Your First LAMP Server on Debian/Ubuntu

Get up and running with a LAMP server for your PHP applications.

  • Use apt-cache and apt-get commands to find packages
  • Install Apache and MySQL
  • Install PHP and all the modules you'll need to get started

Get up and running with a LAMP server for your PHP applications.

  • Use apt-cache and apt-get commands to find packages
  • Install Apache and MySQL
  • Install PHP and all the modules you'll need to get startedWe'll see how to install a LAMP server onto a Debian/Ubuntu server.

Debian/Ubuntu servers both use the apt-get command (APT package manager) to manage software and dependencies.

Install basics

You may or may not use these tools. I do, and install them first on most servers. In this video, these are already included and at the latest version on the Digita Ocean Ubuntu 14.04 server.

# See server information
lsb_release -a

# Install basics
sudo apt-get install -y vim tmux curl wget

Install LAMP

# See available versions to install
sudo apt-cache show apache2
sudo apt-cache show php5-cli

# Install
sudo apt-get install -y apache2 libapache2-mod-php5 \
     mysql-server \
     php5-cli php5-curl php5-mysql php5-mcrypt php5-json php5-gd php5-mysql

Note that I missed installing the php5-mysql package here in the video, but install it later.

Then, check the version of the installed Apache and PHP:

apache2 -v
php -v

We can get more update-to-date versions of Apache and PHP by adding the Ondrej PPA's for Apache and PHP-5.6.

# Get add-apt-repository command
sudo apt-get install -y software-properties-common

# Update/Install Latest Stable Apache (2.4.12)
sudo add-apt-repository -y ppa:ondrej/ppa:ondrej/apache2

# Update/Install PHP (5.6)
sudo add-apt-repository -y ppa:ondrej/php5-5.6

Apache vHost PHP Files

Check Apache virtual hosts that come out of the box. Available ones are in sites-available while enabled ones are symlinked from sites-available to sites-enabled.

sudo vim /etc/apache2/sites-available/default

sudo vim /etc/apache2/sites-enabled/default

We'll create new virtual host at /etc/apache2/sites-available/my_app.conf:

<VirtualHost *:80>
    ServerName my-site.com
    ServerAlias 104.236.197.115.xip.io

    DocumentRoot /var/www/my_app/public
    <Directory /var/www/my_app/public>
        # Don't show directory index
        Options -Indexes +FollowSymLinks +MultiViews

        # Allow .htaccess files
        AllowOverride All

        # Allow web access to this directory
        Require all granted
    </Directory>

    # Error and access logs
    ErrorLog ${APACHE_LOG_DIR}/my-site.error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/my-site.access.log combined
</VirtualHost>

Make sure our DocumentRoot exists:

# -p flag makes any directories it needs to in order
# to complete this file path
sudo mkdir -p /var/www/my_app/public

Enable the Virtual Host using Apache's tool that comes with the Debian/Ubuntu package of Apache2:

# Symlink it to sites-enabled directory
sudo a2ensite my_app

# Inspect the sites-enabled directory
# to ensure there's a symlink to the my_app.conf file
ll /etc/apache2/sites-enabled

# Reload Apache so the new configuration is loaded
sudo service apache2 reload

Create a PHP file at /var/www/my_app/public/index.php:

<?php
phpinfo();

Save that and try it out in the browser to see it. I used URL 104.236.197.115.xip.io.

Check out log files if you'd like (not covered in the video):

sudo tail -f /var/log/apache2/my-site.access.log
sudo tail -f /var/log/apache2/my-site-error.log

MySQL Setup

MySQL was installed in our first few steps. Here we'll create a new database, user and grant that user permissions.

mysql -u root -p
> CREATE DATABASE some_db;
> CREATE USER 'my_app_user'@'%' IDENTIFIED BY 'my_password';
> GRANT ALL PRIVILEGES on some_db.* TO 'my_app_user'@'%';

Note we create a user who can connect remotely or locally, using the wildcard "%" for the host. MySQL won't necessarily allow remote connections just by creating a user, however.

In general the steps to allowing external MySQL connections are (not covered in this video, but noted here):

  1. Bind MySQL to listen on all networks. In /etc/mysql/my.cnf, set bind-address=0.0.0.0.
  2. Set firewall (usually iptables) to allow incoming TCP connections on port 3306
  3. Create a MySQL user who can connect remotely and grant that user privileges on appropriate databases.

Above we only did step 3. You may want to create a user like this instead:

> CREATE USER 'my_app_user'@'localhost' IDENTIFIED BY 'my_password';
> GRANT ALL PRIVILEGES on some_db.* TO 'my_app_user'@'localhost';

Connect to DB

Lastly we'll create a PHP file to test the MySQL connection and user setup. Edit our /var/www/my_app/public/index.php file:

<?php
$dsn = 'mysql:dbname=some_db;host=localhost';
$user = 'my_app_user';
$password = 'my_password';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (\PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

echo "Connected!";

If all went well, you should see the word "Connected" when reloading the index.php page in your browser. In the video, I discovered that I missed installing the php5-mysql package and had to install it and restart apache to get the updated PHP loaded in.

sudo apt-get install -y php5-mysql
sudo service apache2 restart

All Topics