Keith Smith was first introduced to programming at the University of Arizona in 1983. From 1986 to 1990 Keith was an xBase developer. In 2000 Keith built his first website from scratch by hand. In 2006 Keith become a freelance PHP developer. Today Keith wants to teach you everything he knows about PHP programming.
The Apache web server, serves content from a server to a browser. Apache is widely used on the the web. In this article I will cover most, if not all, of the Apache commands you will need to know.
Start Apache Service
sudo systemctl start apache2 # For Debian-based systems (Ubuntu)
sudo systemctl start httpd # For Red Hat-based systems (CentOS, Fedora)
Stop Apache Service
sudo systemctl stop apache2 # For Debian-based systems (Ubuntu
sudo systemctl stop httpd # For Red Hat-based systems (CentOS, Fedora)
Restart Apache Service
sudo systemctl restart apache2 # For Debian-based systems (Ubuntu)
sudo systemctl restart httpd # For Red Hat-based systems (CentOS, Fedora)
Reload Apache Configuration
sudo systemctl reload apache2 # For Debian-based systems (Ubuntu)
sudo systemctl reload httpd # For Red Hat-based systems (CentOS, Fedora)
Check Apache Status
sudo systemctl status apache2 # For Debian-based systems (Ubuntu)
sudo systemctl status httpd # For Red Hat-based systems (CentOS, Fedora)
Enable Apache to Start at Boot
sudo systemctl enable apache2 # For Debian-based systems (Ubuntu)
sudo systemctl enable httpd # For Red Hat-based systems (CentOS, Fedora)
Disable Apache to Start at Boot
sudo systemctl disable apache2 # For Debian-based systems (Ubuntu)
sudo systemctl disable httpd # For Red Hat-based systems (CentOS, Fedora)
Test Apache Configuration
apachectl configtest
Check Apache Version
apache2 -v # For Debian-based systems (Ubuntu)
httpd -v # For Red Hat-based systems (CentOS, Fedora)
Stop Apache Gracefully
sudo apachectl graceful
Run Apache in the Foreground (Debug Mode)
sudo apachectl -X
Disable a Site
sudo a2dissite [site-name] # For Debian-based systems (Ubuntu)
Enable a Site
sudo a2ensite [site-name] # For Debian-based systems (Ubuntu)
Check Apache Modules
apache2ctl -M # For Debian-based systems (Ubuntu)
httpd -M # For Red Hat-based systems (CentOS, Fedora)
Enable/Disable Apache Modules
sudo a2enmod [module_name] # Enable a module
sudo a2dismod [module_name] # Disable a module
Conclusion
These commands should help you effectively manage your Apache HTTP Server. Always ensure you have the proper permissions (often requiring sudo) to execute administrative actions.
This article is an introduction to many Linux command line commands. Each command has one or more arguments which I did not include.
This is not a comprehensive list, however this list will get you well on your way.
Here are some fundamental Linux commands that are essential for everyday use:
File and Directory Management:
ls – List directory contents
cd – Change directory
pwd – Print working directory (shows the current directory)
mkdir – Make a new directory
rmdir – Remove an empty directory
rm – Remove files or directories
cp – Copy files or directories
mv – Move or rename files or directories
touch – Create an empty file or update the timestamp of a file
File Viewing and Editing:
cat – Display file contents
more / less – View file contents page by page
head – Display the first few lines of a file
tail – Display the last few lines of a file
File Permissions and Ownership:
chmod – Change file permissions
chown – Change file owner and group
chgrp – Change the group ownership of a file
System Information:
df – Display disk space usage
du – Show disk usage of files and directories
top – Display system processes
ps – Show running processes
free – Show memory usage
uptime – Show how long the system has been running
uname – Show system information
Package Management (Debian-based systems like Ubuntu):
apt-get – Package management tool
Searching:
find – Search for files in a directory hierarchy
grep – Search for text patterns within files
Networking:
ping – Test network connectivity
ifconfig (or ip a) – Show network interface configuration
netstat – Show network connections
scp – Secure copy (copy files over SSH)
Archive and Compression:
tar – Create and extract tar archives
zip / unzip – Compress or decompress files
Other Useful Commands:
sudo – Execute a command as a superuser
man – Show the manual for a command
history – Show the history of previously executed commands
alias – Create custom command shortcuts
Conclusion
In this article we cover a lot of the Linux command line commands. This may not be a comprehensive list of Linux command, however the contents of this article will get you well on your way to managing Linux from the command line.
In this article and associated video, we will cover Linux file and directory ownership and permissions.
Every file and directory on a Linux system is associated with an owner and a group. This is how the system identifies who has control over the files and directories and who is part of the group that can access it.
Owner : The user who owns a file or directory. Customarily the owner has the ability to add/edit/delete files/directories and modify their permissions.
Group : A set of users who share the same permissions for a file or a directory.
Others : Everyone else who is not the owner or a member of the group.
1) Symbolic Notation : when permissions and users are represented by letters.
Permissions:
“r” : Read
“w” : Write
“x” : Execute
“+” : Add a permission
“–” : Remove a permission
“=” : Set exact permissions
Ownership:
Owner : u
Group : g
Others : o
Examples:
Add execute permission for the owner: $ chmod u+x file.txt
Remove write permission for others: $ chmod o-w file.txt
2) Numeric Notation
Each permission corresponds to a number:
“r = 4” : read
“w = 2” : write
“x = 1” : execute
The permissions for each user (owner, group, others) are represented as a three-digit number.
“7” = “rwx” (read, write, execute)
“6” = “rw-” (read, write)
“5” = “r-x” (read, execute)
“4” = “r–” (read)
“3” = “-wx” (write, execute)
“2” = “–w–” (read)
“1” = “–x” (execute)
“0” = “–––” (no permissions)
To set permissions numerically, you add the values for owner, group, and others. For example:
File : $ chmod 644 file.txt
“6” (owner) = `rw-` (read, write)
“4” (group) = `r–` (read)
“4” (others) = `r–` (read)
Directory : $ chmod 755 <directory>
“7” (owner) = `rwx` (read, write, execute)
“5” (group) = `r-x` (read, execute)
“5” (others) = `r-x` (read, execute)
3) File Ownership
Owner : The user who owns a file.
Group : A set of users who share the same permissions for a file.
Others : Everyone else who is not the owner or a member of the group.
4) File Permissions
Read (r) : Permission to open and read the contents of a file.
Write (w) : Permission to modify or delete the contents of a file.
Execute (x) : Permission to execute a file (i.e., run it as a program).
5) Directory Ownership
Owner (User): Each directory in Linux is associated with an owner, which is typically a user. The owner has specific permissions to read, write, and execute files within that directory, depending on the permissions set. The owner is usually the user who created the directory, but ownership can be changed.
Group : In addition to the owner, a directory is associated with a group. The group defines a set of users who share common access rights to the directory. Group members can have different permission levels compared to the owner or other users.
Permissions
Linux uses three main types of permissions for directories:
Read (r): The user can list the contents of the directory.
Write (w) : The user can create, delete, or rename files in the directory.
Execute (x) : The user can navigate into the directory and access files or subdirectories.
These permissions are assigned to three categories of users:
Owner: The user who owns the directory.
Group: Users who are members of the group assigned to the directory.
Others: All other users on the system.
6) Directory Permissions
Read (r) : for directories, this allows listing the files inside the directory.
Write (w) : for directories, this allows adding, deleting, or renaming files within the directory.
Execute (x): for directories, this allows accessing the directory, meaning the ability to enter the directory and work with its file.
7) Changing Ownership
To change the owner and group of a file or directory, you use the `chown` command.
Example : $ sudo chown <newuser>:<newgroup> file.txt (could be a directory).
8) Changing Permissions
To modify the permissions, you use the “chmod” command. Permissions can be set using symbolic notation (letters) or numeric notation (numbers).
Read (“r”) : Allows listing the contents.
Write (“w”) : Allows modifying the directory’s contents (adding/removing files).
Execute (“x”) : Allows accessing files inside the directory (entering the directory).
Example: sudo chmod 755 <directory> (could be a file)
9) Listing Permissions
File: One the command line issue the command “vdir”.
Directories: One the command line issue the command “vdir -id”.
Conclusion
This article and associated video cover Linux file and directory ownership and permissions and how to change them.
Changing file ownership and permissions can be completed by one of two approaches – Symbolic Notation or Numeric Notation.
To change file or directory ownership we use the chown command.
To change file or directory permissions we use the chmod command.
vi is a powerful text editor that runs on Linux and UNIX systems, commonly used for editing configuration files, programming, and general text manipulation. It is known for its efficiency and speed once users are familiar with its commands.
vi is my editor of choice while I am connected to a web server via the Secure Shell (SSH). I am a PHP programmer and have a home lab that consists of using VirtualBox to create virtual machines. These virtual machines are web servers created for PHP development and testing. They are Linux, Apache, MySQL, and PHP (LAMP).
I use Ubuntu servers to create these LAMP web servers. Vi is one of the tools in my toolbox.
Why use vi?
Pre-installed: vi is usually available by default on almost all Linux distributions, making it convenient when working on servers or systems with minimal software installed.
Efficiency: vi allows advanced users to perform complex text editing tasks quickly and with precision.
Lightweight: It is a lightweight editor that consumes minimal system resources, which is ideal for working on remote servers or in environments with limited resources.
The best way to learn Vi is to create a new file and try it out for yourself.
1) VI Operates in Three Main Modes:
Normal Mode: This is the default mode. You can navigate and manipulate text using various commands.
Insert Mode: Add, Edit, Delete, and Copy text. Press the i key to activate insert mode. In insert mode, one can add, edit, delete, copy text, and move text around.
Command Mode: You enter this mode by pressing : (colon key) while in normal mode, to run commands like saving, quitting, or searching.
2) I Only Use a Subset of What is Available.
Type “vi <file-name>” on the command line to edit or create a file.
Pressing the “i” key switches us into Insert mode.
Pressing the “ESC” key switches us out of Insert mode into normal mode.
Pressing the “:” (colon) switches us into command mode.
dd command – Remove a line.
:q To quit when no changes have occurred.
:q! To not save changes.
:w Save and continue editing.
:wq To write (save) and quit (exit editing of file)
:set nu — Display line numbers.
:set nonu – Stop displaying line numbers.
3) Commands I Rarely or Never Use:
yy — Copy the current line. Referred to as yank(ed)
p — Paste the yanked line.
o — New line under the current line.
O — New line above the current line.
A — Append to the end of the line.
a — Append after the cursor’s current position.
I — Insert text at the beginning of the current line.
b — Go to the beginning of the word.
e — Go to the end of the word.
x — Delete a single character. (where the cursor is blinking)
dd — Delete the current line.
[number]dd — Delete X number of lines.
[number]yy — Yank X number of lines.
G — Go to the end of the file.
XG — Go to line X in a file.
gg — Go to the first line in a file.
h — Move left one character.
j — Move down one line.
k — Move up one line.
l — Move right one character.
Conclusion
Vi is the editor I like to use when connected to a remote server. I manage virtual web hosts on VirtualBox. Vi makes it easy to manage all the configurations a LAMP server contains.
In this article we learned that vi has Three Main Mode, is always available on Linux and Unix servers, and is easy to use once some of the features are understood.
I am a PHP developer. I use Linux, Apache, MySQL, and PHP (LAMP) to further that goal.
I have published a number of YouTube videos and blog posts that document configuring a LAMP server for development and testing.
I am using an older HP laptop that is running Windows 10. On that, I have installed VirtualBox.
VirtualBox is a type two hypervisor that allows me to create virtual machines.
In this context, Linux is the operating system of the web server. Most PHP websites run on Linux web servers and are configured as a LAMP stack.
Apache serves web pages to users who request them via browsers, handling HTTP requests and delivering the corresponding web content (HTML files, images, etc.) from a server to a client (usually a web browser).
MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) to manage and interact with databases. It is widely used for storing and retrieving data in a variety of applications, from web development to enterprise-level solutions.
PHP (Hypertext Preprocessor) is a popular general-purpose scripting language that is especially suited to web development. It is a server-side language, meaning that it runs on a web server rather than in the user’s browser, allowing developers to create dynamic web pages and other content.
I have created a private network in my home office. This network uses private non-routable IP addresses and private non-routable domain names.
I have configured two virtual hosts on my server:
1) default.internal.
2) lamp.internal.
I have created two users:
1) default.
2) lamp.
The user named default is the owner of the virtual host that is created by Apache when Apache is installed. This virtual host is incomplete. I completed the virtual host configuration so I can use it as a model for the virtual hosts I will build in the future. This virtual host can be accessed by a browser by using the private domain, default.internal.
The user named lamp is the owner of the lamp virtual host and is accessed by a browser using the private domain lamp.internal.
Both users were configured to allow them to use SSH (Secure Shell).
I am configuring my server this way so I can access the virtual host remotely with the ability to create, read, update, and delete files and directories on the remote server.
To complete the goal of remote editing we need to install and configure PHP-FPM. PHP-FPM stands for PHP FastCGI Process Manager.
SPM will enhance the performance of an Apache web server, however I am using it to enable the ability for remote editing.
My daily driver is a 10 year old Dell that is running Kunbuntu (Linux). Native to Kunbuntu is the Linux terminal that allows me to access a remote server. I also have installed Visual Studio Code on this computer which allows for remote editing of PHP code.
Even though VirtualBox is running on my laptop running Windows 10, any virtual server created using VirtualBox is remote to that Windows configuration.
We start by installing and configuring PHP-FPM. This install is only required once.
In the 2nd part, I will configure an Apache virtual host to take advantage of PHP-FPM.
After completing part 1, any time you want to configure an Apache virtual host to take advantage of the PHP-FPM install, you will only need to follow the 2nd part – where I configure an Apache host to use PHP-FPM.
**********************************************************
**********************************************************
*** Install and Activate PHP-FPM ONLY ***
**********************************************************
**********************************************************
To install, activate, start, and configure PHP-FPM on Ubuntu 24.04 LTS without modifying Apache2 configuration or vhost settings, you can follow these steps. This will ensure that PHP-FPM is running and available for use without interfering with Apache’s default settings.
Step 1: Upgrade and Update all of the server packages
Step 2: Create the configuration file for the lamp user:
– sudo vi /etc/php/8.3/fpm/pool.d/lamp.conf
Add the following content:
[lamp] ; Define the pool name, which will be referenced by PHP-FPM
user = lamp ; Set the user under which PHP-FPM will run
group = lamp ; Set the group under which PHP-FPM will run
listen = /run/php/php8.3-fpm.lamp.sock ; Define the socket path for the pool (for communication with the web server)
listen.owner = www-data ; Set the owner of the socket file
listen.group = www-data ; Set the group of the socket file
listen.mode = 0660 ; Set the permissions for the socket file (660 = read/write for owner/group)
pm = dynamic ; Enable dynamic process management (adjusts worker count based on demand)
pm.max_children = 50 ; Maximum number of child processes to handle requests
pm.start_servers = 5 ; Number of child processes created on startup
pm.min_spare_servers = 5 ; Minimum number of idle servers to keep alive
pm.max_spare_servers = 10 ; Maximum number of idle servers to keep alive
pm.max_requests = 5000 ; The maximum number of requests a child process should handle before being restarted
php_admin_value[error_log] = /var/log/php8.3-fpm/lamp_error.log ; Set a custom error log for this pool
php_admin_value[log_errors] = on ; Enable error logging
env[PATH] = /usr/local/bin:/usr/bin:/bin ; Define the environment variables (add more if needed)
** The bolded values should be changed to the user for the virtual host you are configuring.
** Remove all spaces from the left of this code or you will receive an error.
Step 3: Set ownership, permissions, and home directory
Step 7: Ensure that both domains are configured correctly and accessible in your browser:
– Make sure the site is viewable in a browser : http://lamp.internal/info.php
– Look for “Server API” with the value: “FPM/FastCGI”
– Connect via Filezilla.
– Use PuTTy or a Linux Konsole to verify the SSH connection is working.
– Use Visual Studio Code to verify that code can be edited.
– Use Visual Studio to create a PHP script that will create a text file, proving PHP is working correctly under PHP-FPM: Using Visual Studio code create a new file and name it : php-tester.php. Copy and paste the following code into that file and save it. Then run it in your browser : http://lamp.internal/php-tester.php
<?php // The name of the file to be created $filename = "test_file.txt"; // Open the file for writing (creates file if it doesn't exist) $file = fopen($filename, "w"); // Check if the file is created successfully if ($file) { fwrite($file, "This file was created by PHP!"); fclose($file); echo "File '$filename' was successfully created!"; } else { echo "Failed to create the file."; } ?>
– If all is working as expected you should find a file by the name of test_file.txt with the contents of “This file was created by PHP!”.
Conclusion
In this article we covered how to install PHP-FPM and configure it to work with an Apache virtual host.
PHP-FPM serves two purposes: 1) is it a performance enhancer, 2) it provides a way for us to remote edit our PHP code directly on the server.
Installing and configuring PHP-FPM is only required once. That would be the first part which consists of 9 steps.
The second set of 7 steps has to be completed every time you want to configure a new virtual host.
I do this so I will land in the docroot of the virtual host when using SSH, FileZilla, and/or Visual Studio Code. This configuration aids me when I want to edit PHP remotrely using Visual Studio Code.
The command sudo usermod -d is used to change the home directory of a user on a Unix-like operating system. In this case I use this command on Ubuntu 24.04LTS.
Here’s a Breakdown of the Command
sudo: This command runs the following command with superuser (root) privileges, which are often necessary for modifying user accounts.
usermod: This is the command used to modify user account settings.
-d <directory-path>: This option specifies the new home directory for the user.
username: This is the placeholder for the actual username of the account whose home directory you want to change.
Apache configures a default virtual host as part of the initial installation. This virtual host is incomplete as a virtual host. I completed the build of the default Apache virtual host.
As part of the build out I added a user named default and changed it’s home directory to /var/www/html by the following command:
– sudo usermod -d /var/www/html default
Key Points
Changing Home Directory: This command changes where the user’s files and settings are stored. By default, a user’s home directory is often located in /home/username.
Permissions: Ensure that the new directory (<directory-path>) has the appropriate permissions for the user to access and manage files there.
Impact: Changing the home directory can affect scripts, applications, and settings that rely on the old home directory path.
Make sure to back up any important data before making such changes.
To create an SSH user on Ubuntu 24.04lts and restrict access using the AllowUsers directive without using SSH keys, follow these steps:
sudo adduser <user-name>
Follow the prompts to set a password and fill in any additional user information.
2. Update SSH Configuration: Open the SSH configuration file in a text editor:
sudo vi /etc/ssh/sshd_config
3. Find the #AllowUsers line (if it exists) and uncomment it. Then, add your new user to the list. If there is not a line containing AllowUsers add it.
For example:
AllowUsers user1 user2 newuser
If there are other users listed, you can separate them with spaces:
4. Password Authentication: Make sure password authentication is enabled in the SSH configuration.
Uncomment “PasswordAuthentication yes” by removing the hashtage from in front of it.
5. Restart the SSH Service: After saving the changes, restart the SSH service to apply them:
sudo systemctl restart ssh
6. Test SSH Access: From a different terminal or machine, try to SSH into your Ubuntu server using the new user:
ssh newuser@your_server_ip
You should be prompted for the password you set earlier.
7. Important Considerations
Firewall Rules: Ensure that your firewall allows SSH connections (typically on port 22).
Security: Using password authentication can be less secure than using SSH keys. Consider setting up key-based authentication if possible.
That’s it! You’ve created an SSH user and restricted access using AllowUsers.
The .htaccess file is a configuration file used by the Apache web server to manage settings on a per-directory basis. It allows you to override global settings without modifying the main server configuration file. Common uses for .htaccess include:
URL Rewriting: Redirecting URLs or creating cleaner, more readable URLs using the RewriteRule directive.
Access Control: Restricting access to specific files or directories with directives like Require or Deny.
Custom Error Pages: Specifying custom pages for error responses (e.g., 404 Not Found) using the ErrorDocument directive.
Caching: Controlling caching behavior for resources with Expires or Cache-Control headers.
Security: Enhancing security by blocking specific IP addresses or preventing directory listing.
To use .htaccess, simply create the file in the desired directory and add the necessary directives. Make sure that the Apache server is configured to allow .htaccess overrides with the AllowOverride directive in the main configuration file.
<IfModule mod_rewrite.c> : Rewrite rules will only be applied if the mod_rewrite module is loaded and enabled.
RewriteEngine On : Enable the rewrite engine. The RewriteEngine allows you to create complex URL manipulations, such as rewriting URLs for cleaner web addresses, redirecting users, or controlling access based on specific conditions.
The RewriteBase directive in an .htaccess file is used in conjunction with mod_rewrite to specify the base URL for rewriting rules.
RewriteCond %{REQUEST_FILENAME} !-f : If the requested path is not a directory, it rewrites the request to index.php, passing the original path as a query parameter.
– RewriteCond %{REQUEST_FILENAME} !-d : If the requested resource is not a directory, the request will be rewritten to index.php, passing the original request path as a query parameter.
RewriteRule ^(.*)$ index.php/$1 [L] : Here’s a breakdown of the rule:
^(.*)$ : This matches the entire requested URL path. The .* captures any characters (except for line breaks), and the parentheses create a capturing group.
index.php/$1 : This indicates that the request should be rewritten to index.php, followed by the content captured by the first group (i.e., whatever was matched by (.*)).
[L]: This flag tells Apache to stop processing any further rewrite rules if this rule matches. It effectively means “this is the last rule to be processed.”
</IfModule> :Closes the check if mod_rewrite is loaded.
I use this .htaccess because I am planning on creating a simple Mode-View-Controller framework.
This particular configuration requires the Apache2 mod_rewrite be installed and enabled.
Use the PHP function $_SERVER[‘REQUEST_URI’] to collect the URI as a string that can be processed by the PHP code.
Given a URL of https://www.example.com/user/add/ – by using $_SERVER[‘REQUEST_URI’] in the index.php file the $_SERVER[‘REQUEST_URI’] will return a string “/user/add/”.
In M-V-C this would translate into load and run the class user. Then run the method/function add. Basically the code would show a form to create a new user.
Conclusion
In this article we cover that the .htaccess file is a configuration file used by the Apache web server to manage settings on a per-directory basis. It allows you to override global settings without modifying the main server configuration file. I show the .htaccess file I use, with explanations, for my homegrown Model-View-Controller framework that grabs the URI using the PHP $_SERVER[‘REQUEST_URI’] function.
– Access to an Ubuntu LAMP web server running Apache2 with root/sudo access
– Run the following commands:
– sudo apt update
– sudo apt upgrade
– sudo apt autoremove
– sudo reboot
Introduction
I will use the IP of my server which is 192.168.1.81 and two local-non-routable domain names for this tutorial. The IP address of 192.168.1.81 is a private IP address, which means it is not accessible from the Internet. I’ll be using default.internal and lamp.internal domain names. .internal is not accessible from the Internet. .internal is a local-non-routable Top Level Domain Name (TLD).
By using a private IP address for my server and using .internal I can create a simple network that I run out of my home office.
You will need to use your server’s IP address in place of mine. You will also need to use the domains you have decided to use to replace the .internal domain names that I use.
All of this will make more sense once the tutorial has been completed.
I use Ubuntu 24.04LTS as my server operating system and this tutorial is oriented towards Ubuntu and Apache2.
I fully configure the Apache2 default virtual host so it can be copied and modified to create other virtual hosts. This makes life easier.
This tutorial is broken down into two separate parts and assumes you are following along with a fully functional Ubuntu 24.04LTS server that is configured to run Apache2 and the PHP programming language. We will not be accessing the MySQL data server, however it should be installed and be fully functional.
Part 1 : Build out the default Apache virtual Host
Step 1 Ensure That the Required Modules Are Enabled
You typically need mod_ssl for SSL support (if using HTTPS) and mod_rewrite for URL rewriting.
– sudo a2enmod rewrite
– sudo a2enmod ssl
Step 2: Configure the Default Apache Virtual Host file 000-default.conf
Step 10: Check Your Apache Configuration for Syntax Errors
– sudo apache2ctl configtest
If there are no errors, you should see Syntax OK.
Step 11: Enable the new Host
– sudo a2ensite lamp.internal
Step 12: Restart Apache to Apply the Changes
– sudo systemctl restart apache2
Step 13: Update Your Hosts File
For local testing, you can edit your Windows/macOS/Linux hosts file to point your domain names to your local server. Open the file:
– Windows:
– Open Notepad as administrator
– c:/windows/system32/drivers/etc/hosts
– Add 192.168.1.81 lamp.internal
– Linux/macOS:
– sudo vi /etc/hosts (edit Kubuntu using Putty)
– Add 192.168.1.81 lamp.internal
Step 14 : Test Using a Browser
– Test with the IP address of your server
– Test using the local-non-routable domain name
– Test using the lamp.internal/controller/action/arg1 domain name
– Test using the lamp.internal/info.php domain name
This proves everything is working correctly to include PHP and mod_rewrite
Conclusion
In this article and associated YouTube videos we learned how to completely configure the Apache2 default virtual host. We learned how to configure and test the Apache2 module mod_rewrite. We learned how to configure the mod_rewrite .htaccess file. We also learned how to copy the Apache2 virtual host configuration to make a new virtual host on the same server.
In this article we will cover how to fix the Apache Error AH00558 : Could not reliably determine the server’s fully qualified domain name.
This typically indicates that Apache is having trouble determining the server’s fully qualified domain name (FQDN).
In this tutorial, I use the IP address of 182.168.1.81 and [domain].internal. Replace my IP address of 192.168.1.81 with the IP you use on your server. As for the usage of [domain].internal, .internal is a top-level domain I (TLD). This domain is not routable and therefore cannot be accessed by the public Internet. When you see the reference to [domain].internal you can replace the [domain] part with the domain of your liking. You can also use another private/non-routable TLD.
I am running Apache2 on Ubuntu 24.04 LTS. This server is configured as a LAMP server (Linux, Apache, MySQL, and PHP). This configuration is common for most hosting servers.
I am a PHP developer and use VirtualBox to create Ubuntu LAMP servers for development and testing.
I spend most of my Linux time on the command line.
During one such session, I ran the command sudo systemctl restart apache2. As a result, the server responded : AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message.
To alleviate the Apache error AH00558 we will follow the 9 steps listed below.
1) Backup the original apache2.conf file. It is always a good idea to make a copy of any configuration file before modifying it.
The first step is to change directories to the Apache2 directory. Run this command cd /etc/apache2/. Then run this command sudo cp apache2.conf apache2.conf-original to make a copy of the original apache2.conf file.
2) Edit the Apache Configuration File following the 3 steps below:
– sudo vi /etc/apache2/apache2.conf
– set the ServerName directive by entering it into the Apache configuration file. The directive looks like this : ServerName default.internal
– Save and Close the file
3) Run the command sudo apache2ctl configtest to verify Apache is configured correctly and to verify the error message is gone.
4) Restart Apache by running: sudo systemctl restart apache2
5) Verify the change by running the command: sudo tail -f /var/log/apache2/error.log
At this point, you have corrected the error message and can stop here. If you are still experiencing issues jump down to section 9) Test the Apache configuration and follow the directions there.
I added the following steps to complete setting the ServerName directive in the default virtual host.
I also added the part on how to use the Windows/Linux/macOS hosts file so you can create a private network where your servers are on a private/non-routable IP address that utilizes a non-routable/private top-level domain (TLD). I felt this tutorial would not be complete without this information.
6) Configure the ServerName directive for the default virtual host by adding the ServerName directive to the virtual host configuration file. Follow these steps:
– Run this command: cd /etc/apache2/sites-available/
– Then run this command sudo vi 000-default.conf.
– Add the ServerName directive ServerName default.internal to the 000-default.conf file.
– Your 000-default.conf configuration file should look like this:
<VirtualHost *:80>
# The ServerName directive sets the request scheme,
# hostname and port
# that the server uses to identify itself.
# This is used when creating
# redirection URLs. In the context of virtual hosts,
# the ServerName
# specifies what hostname must appear in the request's
# Host: header to
# match this virtual host. For the default
# virtual host (this file)
# this value is not decisive as it is used as a
# last resort host
# regardless. However, you must set it for any
# further virtual host
# explicitly.
# ServerName www.example.com
ServerAdmin webmaster@localhost
ServerName default.internal
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug,
# info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the
# loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/[host].error.log
CustomLog ${APACHE_LOG_DIR}/[host].access.log combined
# For most configuration files from
# conf-available/, which are
# enabled or disabled at a global level,
# it is possible to
# include a line for only one particular virtual host.
# For example the
# following line enables the CGI configuration
# for this host only
# after it has been globally disabled
# with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Don’t forget to replace [host] with your domain name.
There is some valuable information in this file that is worth reading.
7) Configure the Windows/Linux/macOS hosts file. In this case I will show the steps necessary to modify Windows 10.
– Right mouse click on Notepad.
– Run Notepad as administrator.
– Open the hosts file : c:/windows/system32/drivers/etc/ [set drop down to show all files]and select the hosts file.
– add 182.168.1.81 default.internal to the end of the file.
– save and close
For Linux and macOS, add the IP and domain name to the hosts file by running the command sudo vi /etc/hosts and add the line 182.168.1.81 default.internal then save and close.
8) Restart Apache by running the sudo systemctl restart apache2 command.
9) Test the Apache configuration
Run the command sudo apache2ctl configtest. If the Apache AH00558 error message persists check :
Verify /etc/apache2/apache2.conf is configured correctly.
Verify /etc/apache2/sites-available/000-default.conf is configured correctly.
Run sudo tail -f /var/log/apache2/error.log in search of any errors.
Run sudo apache2ctl configtest as many times as it is necessary to get the Apache configuration correct and to remove any error messages.
When all the errors have been addressed, launch a browser and enter the domain you are using into the browser’s URL. At this point, you should see the output from the default virtual host.
Conclusion
In this article we learned how to fix the AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message error message. We learned how to configure the /etc/apache2/apache2.conf configuration file, and the /etc/apache2/sites-available/000-default.conf configuration file. We learned how to configure the hosts files for Windows/Linux/macOS so we can use a non-routable domain name that uses the .internal top-level domain (TLD). We use this TLD in conjunction with a local network that consists of private/non-routable IP addresses.