Documentation

Host your Application in the 1&1 Cloud with XAMPP and Bitnami

Introduction

If you’re a PHP developer building a public-facing Web application, there are a number of good reasons why the cloud should be on your radar. It’s highly scalable, allowing you to quickly scale up if you application turns out to be a hit. It’s cost-efficient, because you only pay for the resources - bandwidth, CPU cycles, memory - you use. And it’s secure, because cloud providers have invested a great deal of time and thought into ring-fencing applications and user data.

However, if you’re new to the cloud or do most of your development locally, getting your PHP application from your local XAMPP box to the cloud can be a bit challenging. That’s where this tutorial comes in. Over the next few pages, I’ll walk you, step by step, through the process of deploying a PHP/MySQL application running on your local XAMPP server, to a cloud server running Bitnami’s LAMP Stack. Keep reading!

What You Will Need

Before we begin, a few quick assumptions. This tutorial assumes that you have a XAMPP installation with a working PHP/MySQL application. It also assumes that you’re familiar with the MySQL command-line client and that you have a working knowledge of transferring files between servers using FTP.

If you don’t have a custom PHP/MySQL application at hand, use the example application included with this tutorial: it’s a simple to-do list, created with Twitter Bootstrap and PHP. You can download it from here.

Now, if you’re new to the cloud, you might be wondering what 1&1 and Bitnami are. Very briefly, 1&1 is a Web hosting provider that makes it easy to launch new virtual servers in the cloud. Bitnami provides pre-packaged applications for these cloud servers, so that you can become productive with them the moment they come online. In short, 1&1 provides the cloud and hosting infrastructure, and Bitnami provides the server software that runs on that infrastructure. 1&1 also offers new users of its 1&1 Cloud Platform 30 days of free usage, so you can easily start experimenting with it without worrying about being billed for usage.

For this tutorial, I’ll be using the Bitnami LAMP Stack, which is Linux-based and bundles PHP, MySQL and Apache, together with key applications and components like phpMyAdmin, SQLite, Memcache, OpenSSL, APC and cURL. The LAMP stack also includes a number of common PHP frameworks, including the Zend Framework, Symfony, CodeIgniter, CakePHP, Smarty and Laravel.

To deploy your application to a 1&1 Cloud Server with the Bitnami LAMP Stack, here are the steps you’ll follow:

  • Register with 1&1

  • Provision a 1&1 Cloud Server with the Bitnami LAMP Stack

  • Validate the cloud server

  • Deploy and test your application on the cloud server

The next sections will walk you through these steps in detail.

Step 1: Register with 1&1

At the end of this step, you will have signed up for a 1&1 Cloud Server.

Begin by visiting the Bitnami website and clicking the "Try the 1&1 Cloud Platform for free" button on the page.

image11

This will redirect you to a Bitnami landing page on the 1&1 website. Find and click the "Click here and order now" button shown below.

image12

This will take you to an order confirmation screen with a 1&1 Cloud Server and 30 days of free usage pre-selected for you. Review the configuration and click the "Continue" button to proceed.

image13

On the next page, create a new 1&1 account by clicking the "Create new account" button.

image14

You’ll now be prompted to enter your name, billing information and password for the 1&1 Control Panel. Click the "Continue to payment" button to proceed.

image15

On the next page, choose whether to pay using a credit card or Paypal, and click the "Continue to Order Review" button to proceed.

image16

Your credit card will be verified and you’ll be presented with the details of your order. Confirm it and your account will be activated. You will be assigned a customer number and you should now be all set to launch your cloud server.

Step 2: Provision a 1&1 Cloud Server

At the end of this step, your 1&1 Cloud Server will be running and you will be able to access it through your Web browser.

To provision your 1&1 Cloud Server:

  • Log in to the 1&1 Control Panel for your country with the customer number and password from the previous step.

  • From the "Control Panel" navigation menu, select the "1&1 Cloud Panel".

    image21
  • Once the 1&1 Cloud Panel loads, navigate to the "Infrastructure → Servers" section.

  • Click the "Create" button.

  • On the resulting page, enter a name for your server and select a server configuration. You can choose from an "S" instance (1 virtual core) to a "5XL" instance (16 virtual cores). For more information, refer to the 1&1 Cloud Server pricing sheet.

    image22
    A "Small" or "S" server will work just fine for most PHP application development tasks.
  • On the same page, in the "Images" section, select the "Applications" tab and choose "LAMP" from the resulting list.

    image23
  • Leave all other fields at their default values. Click "Create" to deploy the new server.

The 1&1 Cloud Panel will now begin the process of creating and deploying the server. A confirmation screen will appear and display the server password.

image24
The server password will not be displayed again, so note it carefully for future reference.

Once the cloud server has been provisioned, it will appear in the list of servers in the 1&1 Cloud Panel and the status indicator will show that it’s running.

image25

The server details section will display the public IP address of the server, together with the URL to the LAMP welcome page.

image26

At this point, you should be able to browse to the cloud server, either by clicking the link in the 1&1 Control Panel (a new browser tab will open) or entering the server IP address directly into your browser’s address bar. You should now see the Bitnami LAMP welcome page, as shown below.

image27

The 1&1 Cloud Panel also includes controls to restart, shut down or delete the server, in the "Actions" menu.

image28

Step 3: Test PHP and MySQL

At the end of this step, you will have logged in to your cloud server and verified that PHP, MySQL and phpMyAdmin are working correctly.

You can now connect to the cloud server and test PHP to make sure it’s working correctly and has all the extensions you need. The easiest way to do this is with PuTTY, a free SSH client for Windows and UNIX platforms.

  • Download the PuTTY ZIP archive from its website.

  • Extract the contents to a folder on your desktop.

  • Double-click the putty.exe file to bring up the PuTTY configuration window.

  • Enter the IP address of your cloud server into the "Host Name (or IP address)" field, as well as into the "Saved Sessions" field.

  • Click "Save" to save the new session so you can reuse it later.

    image31
  • In the "Connection → SSH → Tunnels" section, create a secure tunnel for the phpMyAdmin application by forwarding source port "8888" to destination port "localhost:80".

  • Click the "Add" button to add the secure tunnel configuration to the session.

    image32
  • In the "Connection → Data" section, enter the username 'root' into the "Auto-login username" field.

    image33
  • Go back to the "Session" section and save your changes by clicking the "Save" button.

  • Click the "Open" button to open an SSH session to the server.

  • PuTTY will first ask you to confirm the server’s host key and add it to the cache. Go ahead and click "Yes" to this request.

    image34
  • You will next be prompted for a password. Enter the password noted in the previous step.

You should now be logged in to your cloud server.

image35

By default, the Bitnami LAMP stack includes running Apache and MySQL servers, and all the packages that come with the stack are located in the /opt/bitnami directory. Your first step should be to create a phpinfo.php file in the Apache web server root at /opt/bitnami/apache2/htdocs directory to verify PHP’s capabilities.

shell> cd /opt/bitnami/apache2/htdocs
shell> echo "<?php phpinfo(); ?>" > phpinfo.php

Once the file has been copied, browse to http://[your-cloud-server-address]/phpinfo.php and you should see the output of the phpinfo() command.

image36

With this, you know that your PHP installation is configured and working correctly.

You can also check that MySQL is working by launching the MySQL command-line client at the shell prompt.

shell> mysql -u root -p

When prompted, enter the application password retrieved in the previous step. The client should start up and connect to the local MySQL server, displaying a welcome message as shown below.

image37

You should also be able to access phpMyAdmin through the secure SSH tunnel you created, by browsing to http://127.0.0.1:8888/phpmyadmin.

image38

To log in, use username 'root' with the application password from the previous step.

image39

In case you’d like to troubleshoot errors or modify the configuration for Apache, PHP or MySQL - for example, adjusting the maximum upload file size in PHP or changing the path to the MySQL data directory - here are the locations for key configuration and log files in the Bitnami LAMP Stack:

Configuration file(s)

Log file(s)

Apache

/opt/bitnami/apache2/conf/httpd.conf

/opt/bitnami/apache2/logs/error_log

PHP

/opt/bitnami/php/etc/php.ini

-

MySQL

/opt/bitnami/mysql/my.cnf

/opt/bitnami/mysql/data/mysqld.log

Usually, you’ll need to restart your server(s) for your changes to take effect. The Bitnami LAMP Stack includes a control script that lets you easily stop, start and restart Apache, MySQL and PHP. The script is located at /opt/bitnami/ctlscript.sh. Call it without any arguments to restart all services:

shell> sudo /opt/bitnami/ctlscript.sh restart

Or use it to restart a specific service only by passing the service name as argument - for example 'mysql':

shell> sudo /opt/bitnami/ctlscript.sh restart mysql
image310

Step 4: Deploy the XAMPP Application to the Cloud Server

At the end of this step, your PHP/MySQL application will be running in the cloud.

Your cloud server is now provisioned, secured and has a functional PHP/MySQL environment. All that’s left is for you to transfer your application code from your local XAMPP environment to your cloud server and set up the database.

The easiest way to transfer files to the server is with FTP or SFTP. Although you can use any FTP/SFTP client, I like FileZilla, a cross-platform, open source and feature-rich client. Download it from the FileZilla website and install it using the automated installer - it’s a quick process, only requiring you to agree to the license, choose the components (the default selection is usually fine) and specify the installation directory.

image71

Once FileZilla is installed, launch it and you’ll arrive at the main split-screen interface, one side for your local directories and the other for remote directories.

image72

To connect to the cloud server and deploy your application, follow these steps:

  • Use the "Edit → Settings" command to bring up FileZilla’s configuration settings.

  • Use the "File → Site Manager → New Site" command to bring up the FileZilla Site Manager, where you can set up a connection to your cloud server.

  • Enter your server IP address and 'root' as the user name.

  • Select "SFTP" as the protocol and "Ask for password" as the logon type.

image73
  • Use the "Connect" button to connect to the cloud server and begin an SFTP session. You will be prompted for your SSH password.

  • On the remote server side of the window, change to the /opt/bitnami/apache2/htdocs directory

  • On the local server side of the window, change to the directory containing your application code.

  • Upload your XAMPP application code to the remote directory by dragging and dropping the files from the local server to the cloud server (you can back up the original contents of the directory if you wish, by downloading them first).

image74
  • Once the files are transferred, log in to the server console using PuTTY.

  • Create a database for the application using the MySQL command-line client (you can use phpMyAdmin if you prefer a graphical interface). For example, since the application is a to-do list, let’s call the database 'tasks'.

mysql> CREATE DATABASE tasks;
  • Follow best practices and create a separate MySQL user with privileges to access only this database.

mysql> GRANT ALL ON tasks.* TO 'tasks'@'localhost' IDENTIFIED BY 'klio89';
image75
  • If required, update database credentials in your application. Then, install the application schema in the new database (assuming you already uploaded it with the application code). For example, you can use the following command with the MySQL command-line client:

shell> mysql -u tasks -D tasks -p < schema/tasks.sql
image76

If you’re logged in to phpMyAdmin, you can also import the database schema from your local XAMPP system. To do this, select the "Import" tab of the phpMyAdmin dashboard, select the file containing the schema, and click "Go" to have the tables created in your selected database.

image77

Browse to your cloud server’s host name and your application should be active. Here are a few screenshots of the example to-do list application running on the cloud server.

image78

Congratulations! You’ve successfully deployed your XAMPP application in the cloud.

Understand 1&1’s Dashboard and Monitoring Tools

To help you get the most out of your cloud server, 1&1 makes a number of administrative and monitoring tools available. These help you keep track of your application’s performance in the cloud and optimize your server and software configuration as needed.

To see these tools in action:

  • Sign in to the 1&1 Cloud Panel if required using your existing account.

  • Navigate to the "Security → Monitoring Center" section.

  • Select your cloud server from the resulting list.

Once you do this, you’ll arrive at the 1&1 dashboard for your cloud server. Select the "Charts" menu item to see an overview of CPU usage, disk activity and network traffic over the last 1 day, 7 days, 30 days or 365 days.

image81
In case your server doesn’t have a monitoring policy, you can activate this by browsing to the "Infrastructure → Servers" page and choosing from the "Monitoring Policies" section on your server detail page.
image82

You can also use the 1&1 dashboard to resize your virtual server. To do this:

  • Navigate to the "Infrastructure → Servers" section.

  • Select the server you wish to modify from the list of servers.

image83
  • In the server detail section, find the "Configuration" field and click it. This will produce a dialog listing the currently selected server type and other available types.

  • Choose the new server type and click "Confirm Changes", then "Yes" when prompted for reconfirmation.

image84

The server should restart using the new type.

Improve Application Performance

Web application performance problems are hard to debug at the best of times, and more so when your server is in the cloud and running a pre-packaged stack. The responsiveness of your application at any given moment depends on numerous factors: server type, network bandwidth, cloud provider load, database load, caching system in use, application code structure, query structure and various other variables.

The Bitnami LAMP Stack already uses the Apache Event MPM and PHP-FPM for reduced memory usage and an increase in the number of simultaneous requests that the server can handle (more information). It also comes with the mod_pagespeed Apache module activated to rewrite pages on the fly and improve latency.

If you’re finding that your PHP/MySQL application’s performance is not up to scratch, here are a few general tips you can consider:

  • The Bitnami LAMP Stack includes APC, a popular PHP bytecode cache. Usually, when a PHP script is executed, the PHP compiler converts the script to opcodes and then executes the opcodes. APC provides a framework for opcode caching, thereby speeding up PHP applications without needing any code changes. Make sure your APC cache has enough memory and a long TTL. Read more about APC and how to use APC with PHP and Bitnami.

  • The Bitnami LAMP Stack also includes the PHP memcache extension. Memcache is a high-performance, distributed memory object caching system. Consider using memcache to store frequently-accessed fragments of data in memory as arrays, thereby reducing the load on your MySQL database server. Read more about memcache in PHP and how to use memcache with PHP and Bitnami.

  • Turn on MySQL’s slow query log and set MySQL’s 'long_query_time' variable to a low number. This lets you track which of your queries are performing inefficiently and adjust them, either structurally or by applying table indexes as needed, to improve performance. You can use tools like mysqldumpslow or mysql-slow-query-log-visualizer to parse and analyze the slow query logs generated.

  • If your application is database-heavy, you’ll gain performance by giving the MySQL server more memory. Use a tool like MySQLTuner to identify which server parameters need tuning, and incrementally make changes to your server’s cache and buffers to improve performance. If your tables are all MyISAM, disable InnoDB in your my.cnf file to save further memory.

  • Unload Apache modules which you don’t need to save memory, and adjust the log level to errors only.

  • Minify your JavaScript code, and consider using a CDN for static content like images.

Good luck, and happy coding!

About the author

Vikram Vaswani is the founder of Melonfire, an open source software consultancy firm, and the author of seven books on PHP, MySQL and XML development. Read more about him at http://vikram-vaswani.in/.