Book Image

Magento PHP Developer????s Guide, 2nd Edition

By : Allan MacGregor
Book Image

Magento PHP Developer????s Guide, 2nd Edition

By: Allan MacGregor

Overview of this book

Table of Contents (16 chapters)
Magento PHP Developer's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

LAMP from scratch


LAMP (Linux, Apache, MySQL, and PHP) is a solution stack of open source technologies that are used to build a web server and are the current standard for the running of Magento.

For a more detailed list of requirements, refer to www.magentocommerce.com/system-requirements.

Note

Although nginx has seen a wider range of adoption among Magento developers, at the time of writing this book, Apache2 is still the community-accepted standard. We will focus on working with it.

As developers, there are multiple challenges and nuances of setting up and maintaining our development environment, such as the following:

  • Matching your development and production environments

  • Maintaining a consistent environment between different platforms and team members

  • Setting up a new environment can take several hours

  • Not all developers have the knowledge or experience required to set up a LAMP server on their own

We can resolve the first two points with the help of Oracle's VirtualBox (www.virtualbox.org). VirtualBox is powerful and widely popular virtualization engine that will allow us to create virtual machines. VMs can also be shared between developers and across all major operating systems.

Getting VirtualBox

VirtualBox is open source, and it is supported across all platforms. It can be downloaded directly from www.virtualbox.org/wiki/Downloads.

Now, we will proceed to set up a Linux virtual machine. We have selected Ubuntu server 12.04.2 LTS for its ease of use and widely available support. First, download the ISO file from www.ubuntu.com/download/server; either 64-bit or 32-bit versions will work.

To create a new Linux virtual machine, perform the following steps:

  1. Start the VirtualBox manager and click on the New button in the top-left corner:

  2. A wizard dialog will pop up and guide us through the steps to create a bare virtual machine. The wizard will ask us for the basic information to set up the virtual machine:

    • VM Name: This defines how we will name our virtual machine; let's name it Magento_dev 01

    • Memory: This is the value of system memory that will be assigned to the guest operating system when our VM starts, in order to run a full LAMP server; 1 GB or more is recommended

    • Operating System Type: This is the type of OS that we will install later; in our case, we want to select Linux/Ubuntu, and depending on our selection, VirtualBox will enable or disable certain VM options

  3. Next, we need to specify a virtual hard disk. Select Create a virtual hard drive now.

  4. There are many hard disk options available, but for most purposes, selecting VDI (which is VirtualBox Disk Image) will suffice. This will create a single file on our host operating system.

  5. We now need to select the type of storage on the physical drive. We are provided with two options:

    • Dynamically allocated: The disk image will grow automatically as the files and usage on our guest operating system grow

    • Fixed Size: This option will limit the size of the virtual disk from the start

  6. Next, we will need to specify the size of our virtual hard disk. We want to adjust the size depending on how many Magento installations we plan to use.

    Note

    In general, we want to keep at least 2 GB per Magento installation and another 3 GB if we are running the database server on the same installation. This is not to say that all this space will be used at once or whether it will even be used at all, but Magento installations can use a lot of disk space once product images and cache files are factored in.

  7. Finally, we just need to click on the Create button.

    Note

    The main difference between the fixed size hard disk and the dynamically allocated hard disk is that the fixed size hard disk will reserve the space on the physical hard drive right from the start, whereas the dynamically allocated hard disk will grow incrementally until it gets to the specified limits.

The newly created box will appear on the left-hand side navigation menu, but before we start our recently created VM, we need to make some changes:

  1. Select our newly created VM and click on the Settings button on the top.

  2. Open the Network menu and select Adapter 2. We want to set this box up as a bridged adapter to our main network interface. This will allow us to connect remotely using SSH.

  3. Go to the System menu and change the Boot Order value, so that the CD/DVD-ROM boots first.

  4. In the Storage menu, select one of the empty IDE controllers and mount our previously downloaded Ubuntu ISO image.

Booting our virtual machine

At this point, we have successfully installed and configured our VirtualBox instance and are now ready to boot our new virtual machine for the first time. To do this, just select the VM in the left sidebar and click on the Start button at the top.

A new window will pop up with an interface to the VM. Ubuntu will take a few minutes to boot up.

Once Ubuntu has finished booting up, we will see two menus. The first menu will allow us to select the language, and the second one is the main menu that provides several options. In our case, we just want to proceed with Install Ubuntu Server:

We should now see the Ubuntu installation wizard, which will ask for our language and keyboard settings. After selecting the appropriate settings for our country and language, the installer will proceed to load all the necessary packages in the memory. This can take a few minutes.

Ubuntu will proceed to configure our main network adapter, and once the automatic configuration is done, we will be asked to set up the hostname for the virtual machine. We can leave the hostname to the default settings.

The next screen will request that we enter the full name of our user; for this example, let's use Magento Developer.

Next, we will be asked to create a username and password. Let's use magedev as our username.

Let's set magento2013 as our password.

On the next screens, we will be asked to confirm our password and set up the correct time zone. After entering the right values, the installation wizard will show the following screen, asking about our partition settings:

In our case, we want to select Guided – use entire disk and set up LVM; let's confirm that we are partitioning our virtual disk.

We will be asked to confirm our changes a final time; select Finish partitioning and Write changes to the disk.

The installation wizard will ask us to select predefined packages to install; one of the available options is LAMP server.

Although this is highly convenient, we don't want to install the LAMP server that comes prepackaged with our Ubuntu CD; we will be installing all the LAMP components manually, to guarantee that they are set up to specific needs and are up to date with the latest patches.

However, for this, we will need an SSH server; select OpenSSH server from the list and click on Continue.

The installation of Ubuntu is complete, and it will reboot into our newly installed virtual box.

We are almost ready to proceed with the installation of the rest of our environment, but first, we need to update our package manager repository definitions, log in to the console, and run the following command:

$ sudo apt-get update

APT stands for Advance Packaging Tool and is one of the core libraries included with most Debian GNU/Linux distributions; apt greatly simplifies the process of installing and maintaining software on our systems.

Once apt-get has finished updating all the repository sources, we can proceed with the installation of the other components of our LAMP server.

Installing Apache2

Apache is an HTTP server. Currently, it is used to host over 60 percent of the websites on the Web and is the accepted standard for the running of Magento stores. There are many guides and tutorials available online in order to fine-tune and tweak Apache2 to increase Magento's performance.

Installing apache is as simple as running the following command:

$ sudo apt-get install apache2 -y

This will take care of installing Apache2 and all the required dependencies for us. If everything has been installed correctly, we can now test it by opening our browser and entering http://192.168.36.1/.

By default, Apache runs as a service and can be controlled with the following commands:

$ sudo apache2ctl stop 
$ sudo apache2ctl start 
$ sudo apache2ctl restart 

You should now see Apache's default web page with the It Works! message.

Installing PHP

PHP is a server-side scripting language and stands for PHP Hypertext Processor. Magento is implemented on PHP5 and Zend Framework, and we would need to install PHP and some additional libraries in order to run it.

Let's use apt-get again and run the following commands in order to get PHP5 and all the necessary libraries installed:

$ sudo apt-get install php5 php5-curl php5-gd php5-imagick php5-imap php5-mcrypt php5-mysql -y
$ sudo apt-get install php-pear php5-memcache -y
$ sudo apt-get install libapache2-mod-php5 -y

The first command installed not only PHP5, but it also installed additional packages required by Magento in order to connect with our database and manipulate images.

The second command will install PEAR, a PHP package manager, and a PHP memcached adapter.

Note

Memcached is high-performance, distributed memory caching system; this is an optional caching system for Magento.

The third command installs and sets up the PHP5 module for Apache.

We can finally test that our PHP installation is working by running the following command:

$ php -v

Installing MySQL

MySQL is a popular choice of database for many web applications, and Magento is no exception. We will need to install and set up MySQL as part of the development stack:

$ sudo apt-get install mysql-server mysql-client -y

During the installation, we will be asked to enter a root password; use magento2015. Once the installation has finished, we should have a MySQL service instance running in the background. We can test it by trying to connect to the MySQL server:

$ sudo mysql -uroot -pmagento2015

If everything is installed correctly, we should see the following mysql server prompt:

mysql>

At this point, we have a fully functional LAMP environment that can be used not only to develop and work on Magento websites, but also for any other kind of PHP development.

Putting everything together

At this point, we have a basic LAMP setup up and running. However, to work with Magento, we would need to perform some configuration changes and additional setup.

The first thing that we will need to do is create a location to store our development sites files, so we will run the following commands:

$ sudo mkdir -p /srv/www/magento_dev/public_html/
$ sudo mkdir /srv/www/magento_dev/logs/
$ sudo mkdir /srv/www/magento_dev/ssl/

This will create the necessary folder structure for our first Magento site. Now, we need to check out the latest version of Magento. We can quickly get the files using SVN.

We would need to install SVN first on our server with the following command:

$ sudo apt-get install subversion -y

Once the installer has finished, open the magento_dev directory and run the svn command to get the latest version files:

$ cd /srv/www/magento_dev 
$ sudo svn export --force http://svn.magentocommerce.com/source/branches/1.9 public_html/

We will also need to fix some of the permissions on our new Magento copy:

$ sudo chown -R www-data:www-data public_html/
$ sudo chmod -R 755 public_html/var/ 
$ sudo chmod -R 755 public_html/media/ 
$ sudo chmod -R 755 public_html/app/etc/

Next, we need to create a new database for our Magento installation. Let's open our mysql shell:

$ sudo mysql -uroot -pmagento2015

Once in the mysql shell, we can use the create command, which should be followed by the type of entity (database or table) we want to create and the database name in order to create a new database:

mysql> create database magento_dev;

Although we could use the root credentials to access our development database, this is not a recommended practice to follow because it could compromise not only a single site, but also full database server. MySQL accounts are restricted based on privileges we want, in order to create a new set of credentials that has privileges limited to only our working database:

mysql> GRANT ALL PRIVILEGES ON magento_dev.* TO 'mage'@'localhost' IDENTIFIED BY 'dev2015$#';

Now, we need to properly set up Apache2 and enable some additional modules; fortunately, this version of Apache comes with a set of useful commands:

  • a2ensite: This creates symlinks between the vhosts files in the site-available folder and the sites-enabled folder in order to allow the Apache server to read these files.

  • a2dissite: This removes the symlinks created the by the a2ensite command. It effectively disables the site.

  • a2enmod: This is used to create symlinks between the mods-enabled directory and the module configuration files.

  • a2dismod: This will remove the symlinks from mods-enabled. This command will prevent the module from being loaded by Apache.

Magento uses the mod_rewrite module to generate URLs. The mod_rewrite module uses a rule-based rewriting engine to rewrite request URLs on the fly.

We can enable mod_rewrite with the a2enmod command:

$ sudo a2enmod rewrite

The next step will require that we create a new virtual host file under the site-available directory:

$ sudo nano /etc/apache2/sites-available/magento.localhost.com

The nano command will open a shell text editor, where we can set up the configuration for our virtual domain:

<VirtualHost *:80>
   ServerAdmin [email protected]
   ServerName magento.localhost.com
   DocumentRoot /srv/www/magento_dev/public_html

   <Directory /srv/www/magento_dev/public_html/>
      Options Indexes FollowSymlinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
   </Directory>
   ErrorLog /srv/www/magento_dev/logs/error.log
   LogLevel warn
</VirtualHost>

To save the new virtual host file, press Ctrl + O and then press Ctrl + X. The virtual host file will tell Apache where it can find the site files and what permissions are to be given to it. In order for the new configuration changes to take effect, we need to enable the new site and restart Apache. We can use the following commands to do this:

$ sudo a2ensite magento.localhost.com
$ sudo apache2ctl restart

We are nearly ready to install Magento. We just need to set up a local mapping into our host system host file, using the following for Windows and Unix respectively:

Using Windows, we can do the following:

  1. Open C:\system32\drivers\etc\hosts in Notepad.

  2. Add the following line at the end of this file: 192.168.36.1 magento.localhost.com.

Using Unix/Linux/OS X, we can do the following:

  1. Open /etc/hosts using nano:

    $ sudo nano /etc/hosts
    
  2. Add the following line at the end of this file: 192.168.36.1 magento.localhost.com.

Note

If you are facing problems making the necessary changes on your hosts files, refer to http://www.magedevguide.com/hostfile-help.

We now can install Magento by opening http://magento.localhost.com in our browser. Toward the end, we should see the installer wizard. Follow the steps as indicated by the wizard, and you are set to go!