PHP Apache project with Virtual Host

Antonio Silva - Oct 5 - - Dev Community

Create the Project Directory

First, create a directory for your project. For example, let's create a directory called php:

sudo mkdir /var/www/html/php
Enter fullscreen mode Exit fullscreen mode

Create a PHP Test File

create an index.php file in your project directory:

echo "<?php phpinfo(); " | sudo tee /var/www/html/php/index.php
Enter fullscreen mode Exit fullscreen mode

Set Directory Permissions

Set the appropriate permissions so that Apache can access the directory:

sudo chown -R www-data:www-data /var/www/html/php 
sudo chmod -R 755 /var/www/html/php
Enter fullscreen mode Exit fullscreen mode

The command sudo chown -R www-data:www-data /var/www/html/php performs the following actions:

  1. -R: This option indicates that the operation should be performed recursively, i.e. not only on the specified directory, but also on all its subdirectories and files.

  2. www-data:www-data: Specifies that the user and group that will become the new owners of the files are both www-data. This is a common user and group on Linux systems that serves as the default user for web servers such as Apache and Nginx.

  3. /var/www/php: This is the path of the directory whose properties are being changed.

The command sudo chmod -R 755 /var/www/html/php performs the following actions:

755: This is a permission mode:

  1. The first number (7) gives the owner of the file (or directory) read (4), write (2) and execute (1) permissions, for a total of 7.

  2. The second number (5) gives the group read (4) and execute (1) permissions, but not write permissions, for a total of 5.

  3. The third number (5) also gives other users read (4) and execute (1) permissions, but not write permissions, for a total of 5.

In summary, this command changes the permissions of all files and directories within /var/www/html/php, allowing the owner to have full control (read, write and execute), while the group and other users only have read and execute permissions. This is common in web server environments to ensure that the server can access the necessary files without compromising security.

Create the Virtual Host Configuration File

Create a new configuration file for your Virtual Host. The file should have the same name as the php project:

sudo your_editor /etc/apache2/sites-available/php.conf
Enter fullscreen mode Exit fullscreen mode

Add the Virtual Host Configuration

Add the following configuration to the file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName php.info
    DocumentRoot /var/www/html/php

    <Directory /var/www/html/php/>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/php_error.log
    CustomLog ${APACHE_LOG_DIR}/php_access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Let's analyze each part of the code:

  1. <VirtualHost *:80>

    • This is the beginning of the configuration block for a Virtual Host that listens on port 80, which is the default port for HTTP. The asterisk (*) indicates that the Virtual Host will accept connections from any IP address.
  2. ServerAdmin webmaster@localhost

    • Defines the e-mail address of the server administrator. This e-mail address can be used in error messages or when a user needs to contact the administrator.
  3. ServerName php.info

    • Specifies the name of the server to which this setting applies. In this case, the Virtual Host will respond to requests for php.info. It is important that this name resolves correctly to the server's IP (usually configured in the /etc/hosts file or in DNS).
  4. DocumentRoot /var/www/html/php

    • Defines the directory containing the files that will be served when someone accesses the ServerName (in this case, php.info). In this example, the files are located in /var/www/html/php.
  5. <Directory /var/www/html/php/>

    • This block specifies specific settings for the directory indicated. The settings within this block affect Apache's behavior for that specific directory.

AllowOverride All

  • It allows .htaccess files inside the /var/www/html/php/ directory to override Apache's settings. This means that developers can use .htaccess files to configure rules, such as redirects or access control, without having to edit the main Apache configuration file.

Require all granted

  • Allows all users to have access to the directory. This means that anyone can access the files inside /var/www/html/php/ without restrictions.
  1. ErrorLog ${APACHE_LOG_DIR}/php_error.log
  2. Specifies the path to the Apache error log file for this Virtual Host. The ${APACHE_LOG_DIR} is a variable that is usually set in the main Apache configuration file, pointing to the directory where the logs are stored. Here, errors related to this Virtual Host will be recorded in the php_error.log file.

  3. CustomLog ${APACHE_LOG_DIR}/php_access.log combined

  4. Defines the path to the Apache access log file for this Virtual Host. Like ErrorLog, this also uses the ${APACHE_LOG_DIR} variable. The combined format records information about requests, including the client's IP address, the time of the request, the HTTP method, the URL requested, the status code and the user agent.

Enable the Virtual Host

Enable the new Virtual Host with the command:

sudo a2ensite php.conf
Enter fullscreen mode Exit fullscreen mode

Activate the Rewrite Module (if necessary)

If you need to use .htaccess or URL rewrites, activate the Apache rewrite module:

sudo a2enmod rewrite
Enter fullscreen mode Exit fullscreen mode

Add Server Name to Hosts

To access your project using the server name you defined (php.info), add an entry in the /etc/hosts file:

sudo your_editor /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Add the following line to the end of the file:

127.0.0.1   php.info
Enter fullscreen mode Exit fullscreen mode

Restart the Apache

Restart Apache for the changes to take effect:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Accessing the Project

You can now access your project in the browser by typing http://php.info.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .