Openstack cloud-init Ubuntu

Creating a LAMP/LEMP instance on Fuga Cloud with cloud-init

Creating a LAMP instance

Launching an instance (VM) on Fuga Cloud is very fast thanks to our SSD-drives. But without the software we need, the instance is nearly useless. Of course we can always install our software manually, but that’s cumbersome and error prone. Thankfully the Openstack platform and Ubuntu support cloud-init which enables us to attach an install-script to our instance. So when we launch our instance in Horizon, our software is automagically installed with it. Now we can immediately start focusing on using your software, instead of dealing with all the pains and horror of maintaining a server.

As a front-end developer, I preferably don’t want to have to deal with this as it’s not my area of expertise. When utilizing a cloud-init script, I don’t have to.

Blessing from the Lord

Now, back to the subject at hand. Because of the popularity of the LAMP stack, I’ve chosen to show you how to install a LAMP stack on an Ubuntu 16.04 instance.

Launching an Ubuntu 16.04 instance

Details section

First, we start with logging into Fuga Cloud and click on the Launch Instance button. A popup appears where we will have to fill in the details and specs of the instance in the Details section. Continue by giving your instance a name, ubuntu-lamp for example. For simplicity’s sake, we will leave the rest of the settings in the Details section the default values.

Source section

In the source section, we will pick the Ubuntu 16.04 LTS version. Among some other Linux distros, Ubuntu supports cloud-init.

Flavor section

Next, pick a flavor that will suit your needs. Because I’m Dutch, the smaller the better (read: cheaper).

Networks section

You can pick a network here, I guess any will do.

Security Groups

To be able to create an instance, you need to set up a security group. This is beyond the scope of this tutorial though. Read more about setting up a security group.

Key Pair section

Here you assign or create an SSH-keypair. We will need this to login to our instance from the terminal.

Configuration section

Here we can add our custom cloud-init script. I’ve took the liberty of creating a script you can use to install LAMP to the instance. Here goes:

#cloud-config
apt_update: true
packages:
  - apache2
  - php-mysql
  - mysql-server
  - libapache2-mod-php7.0
  - php7.0-mcrypt
  - php7.0-gd
  - php7.0-curl

Optionally, you can create a gist on GitHub and include the raw Gist like this:

#include
https://gist.githubusercontent.com/vslijkerman/0070af5658bae8501466033784d1988e/raw/3123d54a0a1a88a379e8c7fe6b8e9922a03ccab3/cloudinit-lamp.conf

This way, you can source control your scripts and it’s easier to reuse.

Finally we launch the Instance

With the script attached, we can now press the Launch Instance button. After a few seconds we will now have a configured VM with LAMP on it.

Attach a Floating IP (FIP) to your instance

Next, go to the Instances overview in Horizon and click on the button on the far right of your instance. Choose Associate Floating IP. Select or create a Floating IP and submit.

Start Apache

Next up, we must ssh into our instance. Read more about SSH into an instance, to do just that. When you’re inside your instance, you must now start Apache. You can do this by running:

$ sudo service apache2 start

For completion’s sake I will also give you the commands to stop and restart Apache:

To stop Apache, run:

$ sudo service apache2 stop

To restart Apache, run:

$ sudo service apache2 restart

Find out what the DocumentRoot is

While still inside your instance (SSH), you can navigate to the following directory to see where the Apache HTML root is:

$ cd ../../etc/apache2/sites-enabled
$ cat 000-default.conf

Here you will see that the DocumentRoot is /var/www/html. Now you can navigate to this directory to start modifying the index.html file to your needs.

You should now be able to visit your site by opening the FIP in your browser. You should see the default Apache starting page.

Creating a LEMP Instance

Creating a LEMP instance is almost the same as creating a LAMP instance. I will merely show you the differences here.

Launching an Ubuntu 16.04 instance

Configuration section

Here we can add our custom cloud-init script. I’ve took the liberty of creating a script you can use to install LEMP to the instance. Here goes:

#cloud-config
apt_update: true
packages:
  - nginx
  - php-mysql
  - mysql-server
  - php7.0-mcrypt
  - php7.0-gd
  - php7.0-curl

Optionally, you can create a gist on GitHub and include the raw Gist like this:

#include
https://gist.githubusercontent.com/vslijkerman/d1e62fa52143157f86d8e6489d76ddb2/raw/94695cee252b0fa35d63def477934cdb35cf2a13/cloudinit-lemp.conf

Configuring nginx

First you have to edit the following file by opening nano (you can use whatever you want):

$ sudo nano /etc/nginx/sites-available/default

In this file you have to look for server_name and replace the underscore for your Floating IP. Save the file (in Nano, by pressing ctrl-x, y and enter).

Next you have to start nginx by entering the command:

$ sudo systemctl start nginx

You should now be able to visit your site by opening the FIP in your browser. You should see the default Nginx starting page.

Was this article helpful?


Next article:

Connect to the Fuga Object Store using Python

Fuga supports Amazon S3 compatible object storage. To access the S3 storage you need an EC2 access key and secret key. These can be obtained using the Openstack CLI or the OpenStack Keystone API (which is beyond the scope of this tutorial). Using the OpenStack CLI you can obtain the keypair using the ec2 credentials command as shown below. openstack ec2 credentials list +----------------------------------+----------------------------------+----------------------------------+----------------------------------+ | Access | Secret | Project ID | User ID | +----------------------------------+----------------------------------+----------------------------------+----------------------------------+ | 99999999999999999999999999999999 | 99999999999999999999999999999999 | 99999999999999999999999999999999 | 99999999999999999999999999999999 | +----------------------------------+----------------------------------+----------------------------------+----------------------------------+ When the list is empty a new keypair can be created using the 'openstack ec2 credentials create' command.