Discover multiple ways to troubleshoot Apache HTTP web server on Ubuntu Linux, including checking the server status from the command line.
- [Instructor] There's a few more moving pieces in the Apache HTTP Web Server. All it take is a typo to mess up the configuration and the server won't start or reload. Don't worry. We've got this covered. The Ubuntu Server Guide on Apache at help.ubuntu.com, is an extremely comprehensive resource, and I recommend it in addition to these troubleshooting steps. The very first troubleshooting step is to make sure that the port is forwarded. From the VirtualBox Manager, select Sandbox, then click Settings>Network>Advanced>Port Forwarding.
Apache should be here with a Host Port of 8080 and a Guest Port of 80. If not, either add the row by clicking the + or edit the individual ports so they match. When ready, click OK and OK again. Let's turn off the web server to see what it's like. Using your preferred SSH client, ssh into the server. I'm going to intentionally stop the Apache service. I'm going to type sudo service apache2 stop.
I'll be prompted for my password, and it's done. If I switch over to a browser, and I go to sandbox.dev, we'll say the WordPress site, I'll see a message saying sandbox.dev didn't send any data. This is, in part, because of browser caching. The browser knows that the server existed at one point, but can't connect to it. In contrast, if I go to a server that doesn't exist, I get a message that the server's DNS address cannot be found.
From the Terminal, let's turn the service back on, sudo service apache2 start. No error messages are shown. From the browser, let's try a fail safe. What happens if we try to access local host on port 8080? I should be getting a 404 Not Found, with information about the version of Apache. If you get a placeholder, chances are that the wrong sites are enabled.
Let's double-check. To see what sites are currently enabled, perform a directory listing of the Sites Enabled Directory. So, ls -la/etc/apache2/sites-enabled. There should only be one entry here for vboxsf. If vboxsf is missing, or default is showing, then a step or two was missed in the Apache configuration back in chapter four, video five.
What happens if there's a type in the configuration? Let's intentionally break the configuration. Type sudoedit /etc/apache2/sites-available/vboxsf.conf. I'm going to create a new line by pressing Enter going up and then adding the word pancakes at the beginning of the file. Press Control+x to exit, y, and then Enter to save.
Reload the server configuration using sudo service apache2 reload. Well, there's your problem. Fortunately, it's telling us to see the details, the systemctl status and journal. Let's do the first command. Systemctl status apache2.service. This is pretty explicit about what went wrong, and if I scroll down just a little bit, specifically, the syntax error on line one, and an invalid command, pancakes.
That seems reasonable, so let's fix it. Press q to exit and then sudoedit /etc/apache2/sites-available/vboxsf.conf. Remove pancakes by pressing Control+k. Press Control+x to exit, then y and Enter to save. Reload the good server configuration using sudo service apache2 reload.
This time, there are no errors and the configuration is reloaded cleanly. A great resource for debugging information are Apache's log files, which are located in /var/log/apache2. From the Terminal, change directory to /var/log/apache2. Let's list the directory contents, ls -la. Access.log and error.log are the default server logs. The vboxsf files are for the shared folders.
The tail command can be used to follow a file, in real time, with a -f flag. Let's use it to follow one of the log files. From the Terminal, let's follow the access log. Type tail -f and then vboxsf-access.log. Then at the same time, in a browser window, let's visit the Sandbox. I should see new entries in the access log.
To stop following, press Control+C. There's another way to see if Apache is actually running. The process status command can be used to look for Apache. Let's use ps aux | grep apache2. A number of Apache processes are shown. If the only thing showing is grep, then Apache isn't running. There's another Command, netstat, that can be used for showing network connections, routing tables, interface statistics, and more.
Netstat takes a couple of options, similar to ps. We're going to use n for numeric ports, a for all sockets, and p for program ID and the program. Remember it like, now. This command requires root privileges. From a terminal, type sudo netstat -nap | grep apache. Apache is currently listening to both port 80 and 8080, which is how we configured it.
Let's move on to troubleshooting the MySQL database.
Note: The demonstrations use the Ubuntu distribution of Linux, but the skills are applicable to other Linux distributions.
- What is Linux and why should you use it?
- Choosing a Linux distribution
- Creating a virtual machine (VM)
- Managing Linux from the command line
- Configuring a Linux server
- Managing Apache sites and modules
- Configuring the MySQL database
- Configuring PHP for development
- Debugging PHP code in Linux
- Installing PHP tools such as Composer and CodeIgniter
- Managing VMs with VirtualBox
- Troubleshooting LAMP