Create Windows virtual hosts for your Magento projects
This post is written to help out beginners that are planning to or have just started their web development adventure. It is not the only way of managing your local environment, but it is one of the better ones.
After working on Magento projects for a longer period of time on a local Windows development machine, you can notice mild to heavy performance drop. This is because Magento is a huge system that has a lot of small files and a lot of database tables/entries. The latest Magento CE, for example, contains 11,900+ files and 4,435+ folders by default (not to mention SVN’s files, if you have to work with it or haven’t yet realised the advantages of git). Multiply that by a dozen of projects (active ones and in support phase) and you’ll have some serious fragmentation issues. SSD disks are good, but personally, I don’t yet see them as a reliable solution.
Create Windows virtual host
… and create it on a separate partition. One partition for each project you are working on. You will achieve another thing with using partitions – higher possibility of data recovery in case of system failure (Murphy’s law: Very important files that are not tracked with version control system will die first). Here’s step-by-step guide:
- Create a new partition by using your favourite partitioning tool. Let it be 2 times bigger than what you estimate your project’s size would be (3 times if you are using SVN). Create a new folder that will hold your Magento project (for example X:\yourmagentoproject\)
- Stop your Apache server and enable showing hidden files in Windows.
- Open your favourite text editing tool as an Administrator. Good old Notepad can do the job just fine. If you already don’t know, even if your Windows user has privileges of an Administrator, you don’t have all the file editing rights automatically. Use Windows search bar to find Notepad, right click the result and pick “Run as Administrator”.
- Open hosts file (default location is “Windows\System32\drivers\etc\“) in your text editor and add your new domain name for your Magento project in a new line at the end of the file
- Open Apache’s main configuration file httpd.conf. Default location is in conf folder of your Apache installation. Find the following line:
# This should be changed to whatever you set DocumentRoot to.
There is a
block that defines your default document root. Add the following block after that Directory block:
<Directory "X:/yourmagentoproject/"> Options Indexes FollowSymLinks AllowOverride All Order Deny,Allow Allow from all </Directory>
- Open Apache’s virtual hosts configuration file, typically found in your Apache folder at \extra\httpd-vhosts.conf. Add the following to the end of the file:
<VirtualHost *:80> DocumentRoot "X:/yourmagentoproject/" ServerName yourmagentoproject.loc ServerAlias yourmagentoproject.loc DirectoryIndex index.php index.html index.htm not-a-file </VirtualHost>
In case you are wondering, yes… directory index is defined in vhosts file.
- Start Apache.
Moving MySQL data files to a separate partition
Magento has a lot of tables in its database. Each table is saved in its own .frm file inside a folder named after the database name. Every piece of information you add, edit or delete inside Magento admin is saved in the database along with the frontend visiting logs and statistics. You will also often have to work with client’s databases from existing sites that will be deleted when they are no longer needed and replaced with your freshly built ones. That’s a lot of file management tasks that your disk needs to handle. That means a lot of fragmented files that represent database tables and it’s only piling up over time.
Step-by-step guide how to transfer your MySQL data files to a separate partition:
- Create a new partition that will hold your MySQL data files. Don’t forget to make it big enough to hold all the databases you’ll need.
- Stop the MySQL server.
- Locate your MySQL data files folder. By default, it’s in the data folder of your MySQL installation directory. Copy the entire folder (not just the content of it) to the new partition. Your new folder’s location should be something like Y:\data\
- Open my.ini and add/edit the following lines:
#Path to the database root datadir="Y:/data/"
#*** INNODB Specific options *** innodb_data_home_dir="Y:/data/"
- Start the MySQL server
I really hope that this post will be obsolete in a year. That’s a reasonable timespan for SSD manufacturers to resolve the reliability issues of their drives. In the mean time, with this method you can more easily backup and defragment only projects that you are working on, you can even zip the whole project, quickly re-format the partition and unzip the project to get the fresh start. And it’s a neat way of organising your projects.
thank for the reply..
learnt something new… mysql partitining and sharding…
but looks far more complicated than the double click on my batch file … LOL.. i’ll stick with it … thanks again.
What if i want to have multiple data dir and multiple vhost? is this possible?
example Site 1:
document root r:\site1
server name site-1
datadir := M:\data
example site 2:
docuent root f:\mysite
server name my-site
datadir := f:\data
i have this working in only when i manually change the datadir in my.ini every time i need to access the other site. (i have created a batch file to do the swap for me). can i include tag in virtual host or something?
Database partitioning and sharding comes to my mind, but those are not exactly the tools that are simple to maintain. Those are, generally, scalability tools that are used when your database is rapidly growing beyond your resources, or you have a high access rate. Anyway, you can look the both terms up (“Mysql partitioning” and “Mysql sharding”), but I wouldn’t recommend it on a dev environment, unless you are planning to build a system that uses shards and multiple servers for a database.
Got it work now with log file. Thanks a lot.
Thanks Nikola Stojiljkovic for a good article, I tried with this but I would like to have a private log file for only this magento domain, how I can do that?
@IMKate: You can see how to define Error and Access logs and which Apache modules should be on in order to do this at the official Apache documentation page here: http://httpd.apache.org/docs/2.2/logs.html The explanation is pretty straightforward. 😉
Great post, as always. But I think the intermediate/advanced developers use a slightly different setup.
Just wondering, are you guys using Vagrant or VirtualBox in your development?
Thanks, Marius. You are right, advanced users have their own preferred methods of dealing with environment setup and often pick a tool that they feel is the best for their needs. This article was posted to offer an alternative to a virtual machine approach just by using options that are already available on any Windows machine with Apache/MySql servers by default. Target audience for this article are beginners that want a quick way of environment management without use of any specific tools. Over time, developers usually find the best way/tool for themselves.