A free and open-source book on Zend Framework for beginners


2.4. Apache Virtual Host

Now we are almost ready to get our skeleton website live! The last thing we are going to do is configure an Apache virtual host. A virtual host term means that you can run several websites on the same machine. The virtual sites are differentiated by domain name (like site.mydomain.com and site2.mydomain.com) or by port number (like localhost and localhost:8080). Virtual hosts work transparently for site users, that means users have no idea whether the sites are working on the same machine or on different ones.

Currently, we have the skeleton application inside of your home directory. To let Apache know about it, we need to edit the virtual host file.

Virtual host file may be located at a different path, depending on your operating system type. For example, in Linux Ubuntu it is located in /etc/apache2/sites-available/000-default.conf file. For OS- and server-specific information about virtual hosts, please refer to Appendix A. Configuring Web Development Environment.

Let's now edit the default virtual host file to make it look like below (we assume you use Apache v2.4):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /home/username/helloworld/public
    
	<Directory /home/username/helloworld/public/>
        DirectoryIndex index.php
        AllowOverride All
        Require all granted
    </Directory>

</VirtualHost>

Line 1 of the file makes Apache to listen to all (*) IP addresses on port 80.

Line 2 defines the web master's E-mail address. If something bad happens to the site, Apache sends an alert E-mail to this address. You can enter your E-mail here.

Line 4 defines the document root directory (APP_DIR/public). All files and directories under the document root will be accessible by web-users. You should set this to be the absolute path to skeleton application's public directory. So, the directories and files inside public (like index.php, css, js, etc.) will be accessible, while directories and files above public directory (like config, module, etc.) wont' be accessible by web users, which enhances the security of the website.

Lines 6-10 define rules for the document root directory (APP_DIR/public). For example, the DirectoryIndex directive tells Apache that index.php should be used as the default index file. The AllowOverride All directive allows to define any rules in .htaccess files. The Require all granted directive allows everyone to visit the website.

Zend Framework 3 utilizes Apache's URL rewriting module for redirecting web-users to entry script of your website. Please ensure that your web server has mod_rewrite module enabled. For instructions on how to enable the module, please refer to Appendix A. Configuring Web Development Environment.

After editing the config file, do not forget to restart Apache to apply your changes.


Top