There are a few ways to host your PHP code in a local development environment: you can use PHP’s built-in web server, XAMPP, or set up a separate Apache server.
1. PHP’s Built-In Web Server
PHP provides a built-in web server for development and testing purposes. If PHP is installed on your machine, you can start the server using the following command:
php -S localhost:<port number> <the address of the hosted file>
In this setup, the specified file is hosted on your localhost at the designated port.
2. XAMPP
XAMPP is a bundled software package that provides an integrated stack with Apache HTTP Server, a database (usually MySQL or MariaDB), and interpreters for PHP and Perl scripts.
When it comes to hosting your PHP code, all you need to do is configure and start the Apache Server component.
By default, settings like the server root, document root, server name, and default port for the Apache Server are predefined in /etc/lampp/apache/httpd.conf. You can update these configurations according to your needs.
# /etc/lampp/apache/httpd.conf
ServerRoot '<where the XAMPP is installed>'
Listen <port number>
DocumentRoot '<directory of the hosted project>'
ServerName <url>
You can also configure the directory index (the default landing page) by modifying the DirectoryIndex directive.
However, I found XAMPP is a bit difficult to use to me. One reason is that its directory structure differs from the typical Apache2 setup, which can be confusing. Additionally, you must carefully manage the settings across /etc/lampp/apache, /etc/lampp/etc/httpd.conf and /etc/lampp/etc/extra to avoid potential conflicts — something I found quite frustrating.
Moreover, since XAMPP is a bundled stack, it lacks the flexibility you would typically have on a Linux system.
3. Apache2 Server
Using a separate Apache2 server is a cleaner approach and, in my opinion, much more user-friendly. To install Apache2 install on your Linux system, simply update your apt repository and run the following command:
sudo apt install apache2
By default, the server root is located at /var/www and the configuration files are stored under /etc/apache2.
To host your files, organize them into directories and place them under /var/www.
If you want to configure the server to host a specific project, navigate to /etc/apache2/sites-available. You can use the default 000-default.conf as a template to create your own site configuration file. In this file, you should key settings such as the server name, document root, and server admin:
# <project>.conf
ServerAdmin yourname@<domainname>.com
DocumentRoot /var/www/<root>
ServerName <servername>
DirectoryIndex <index file>
Then, activate the virtual host file by running:
sudo a2ensite <project>.conf
At this point, everything should be ready. To start or reload your Apache service, use:
sudo systemctl start apache2.service
# or
systemctl reload apache2.service
Leave a comment