Journey into PHP Wonderland: Unveiling the Magic of Installation, Apache Harmony, and Nginx Duets on Ubuntu
Once upon a time in the world of web development, aspiring coders and seasoned tech enthusiasts alike found themselves on a quest to harness the power of PHP. Our story begins with a guide — a map, if you will — to navigate the installation and orchestrate a symphony of Apache and Nginx servers on Ubuntu.
Chapter 1: The PHP Installation Adventure
In the heart of Ubuntu, our heroes embarked on a mission to install PHP, the magical server-side scripting language. Armed with the mighty terminal, they uttered the sacred commands:
sudo apt update
sudo apt upgrade
sudo apt install php-common libapache2-mod-php php-c
// verify php is installed by checking the version
php -v
Lo and behold, PHP revealed its version, signaling a successful installation! The heroes were ready to proceed to the next chapter.
Chapter 2: Dancing with Apache in the Ubuntu Browser Ballroom
In the land of the Ubuntu browser, Apache, the grand server, awaited our heroes. With the following spells, Apache was summoned:
sudo apt install apache2
sudo systemctl start apache2
The Apache server breathed life into a simple PHP file, residing in the sacred /var/www/html
directory:
<?php
echo "Hello, PHP!";
?>
In their browser, our heroes typed the incantation http://localhost/index.php
, and the words "Hello, PHP!" shimmered on the screen – a testament to their successful collaboration with Apache.
Note:
No, all your HTML files do not have to be in the /var/www/html/
directory. The /var/www/html/
directory is the default document root for Apache on many Linux distributions, and it is where the server looks for files to serve by default. However, you can configure Apache to serve files from different directories as well.
Here’s how you can set up a separate folder and configure Apache to serve files from that folder:
- Create a new directory for your project:
sudo mkdir /var/www/myproject
2. Copy your HTML and PHP files into the new directory:
sudo cp /path/to/your/files/* /var/www/myproject/
3. Change ownership of the directory to the Apache user (www-data):
sudo chown -R www-data:www-data /var/www/myproject
4. Edit the Apache configuration file to include your new directory:
- Open the Apache configuration file in a text editor. The file may be named
000-default.conf
,default
, or similar, and it is usually located in/etc/apache2/sites-available/
.
sudo nano /etc/apache2/sites-available/000-default.conf
- Look for the
DocumentRoot
directive and change it to point to your new directory: DocumentRoot /var/www/myproject
Add a <Directory>
block to allow Apache to access the new directory:
<Directory /var/www/myproject>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Save the changes and exit the text editor.
Restart Apache to apply the changes:
sudo systemctl restart apache2
Now, Apache will serve files from your new directory (/var/www/myproject
). You can access your HTML and PHP files by navigating to http://your_server_ip/your_file.html
or http://your_server_ip/your_php_file.php
.
This setup allows you to organize your projects into separate directories, and you can have different projects with their own HTML, PHP, and other files in different directories under /var/www/
. Adjust the permissions and configuration as needed for your specific requirements.
Back to our story…
Chapter 3: The Nginx and Apache Tango on Port 80
As our heroes reveled in their victory, a shadow loomed on the horizon — a conflict between Nginx and Apache over the coveted Port 80. Undeterred, they unravelled the mystery:
sudo lsof -i :80
Identifying the conflicting processes, they gracefully halted either Nginx or Apache:
sudo systemctl stop apache2
To prevent further turmoil, they reconfigured the servers to dance to different tunes on alternative ports. With configurations updated and services restarted, the clash was averted.
Note:
Nginx doesn’t know how to run a PHP script of its own. It needs a PHP module like PHP-FPM to efficiently manage PHP scripts. PHP-FPM, on the other hand, runs outside the NGINX environment by creating its own process.
To run a PHP file via the terminal, you need to use the PHP command-line interface (CLI). Here are the general steps:
- Open a Terminal:
- On Linux or macOS, you can use the terminal or command-line interface.
- On Windows, you can use Command Prompt or PowerShell.
2. Navigate to the Directory: Use the cd
command to navigate to the directory where your PHP file is located. For example:
cd /path/to/your/php/files
Run the PHP File: Use the php
command followed by the name of your PHP file to run it. For example:
php your_file.php
Replace your_file.php
with the actual name of your PHP file.
If PHP is not in your system’s PATH, you might need to provide the full path to the PHP executable. For example:
/path/to/php/bin/php your_file.php
View the Output: The terminal will display the output generated by your PHP script.
Here’s a simple example. Suppose you have a PHP file named hello.php
with the following content:
<?php
echo "Hello, World!\n";
?>
To run this file, you would do the following:
- Open a terminal.
- Navigate to the directory containing
hello.php
. - Run the command:
php hello.php
The terminal should then display the output:
Hello, World!
This is a basic overview, and the exact steps might vary depending on your operating system and PHP installation. If you encounter any issues, make sure that PHP is installed and available in your system’s PATH.
Another option is installing a php extension on your vscode.
Here’s a screenshot of the extension:
Conclusion: A Harmonious PHP Kingdom
And so, our heroes triumphed over the challenges, having conquered the installation of PHP, danced with Apache in the browser ballroom, and resolved conflicts between Nginx and Apache on Port 80. Their journey into the PHP Wonderland left a trail for others to follow, a testament to the magic that unfolds when code and creativity intertwine.
Embark on your own PHP adventure, fellow developers, and may your code be ever elegant and your servers forever harmonious! ✨🚀