Linux:Powerful Server Administration
上QQ阅读APP看书,第一时间看更新

Troubleshooting the web server

In this recipe, we will cover some common issues with Apache and Nginx and list the basic steps for overcoming those issues. The steps mentioned here are general troubleshooting methods; you may need to change them based on your setup and environment.

Getting ready

You may need root level access to your web server system.

How to do it…

Web server problems can be grouped in a few broad categories, such as a server not working, a particular domain or virtual host is not accessible, problems with a specific module configuration, and access denied errors. The following section lists each of these problems and their possible solutions.

Web server not accessible
  1. The first step is to check your local Internet connection. Try to access the server from another system from another network.
  2. Check if the DNS settings point to your web server.
  3. If your network is working properly, then try to ping to the server IP address.
  4. On the web server, check the firewall or any other tool that may block communication.
  5. Open a telnet connection to web server on port 80, or whatever port you have used for web server. If you see output similar to following screenshot, then your web server is working:
  6. Make sure that the web server port is not being used by some other process:
    $ sudo netstat -plutn
    
  7. If required, reload or restart the web server process:
    $ sudo service apache2 reload/restart
    
  8. Check the Apache/Nginx logs listed under the /var/log/ directory and view the entire file in a scrollable format:
    $ less /var/log/apache2/error.log
    
  9. See the continuous stream of logs as they are added to the log file:
    $ tail -f /var/log/nginx/error.log
    
  10. You may want to run Apache with extended log levels. Find the variable LogLevel in /etc/apache2/apache2.conf and set its value to debug:
    $ sudo nano /etc/apache2/apache2.conf
    LogLevel debug
    
  11. Run Apache in debug single process mode:
    $ sudo apache2ctl -X # debug mode single worker
    
Virtual host not accessible
  1. Make sure you have enabled virtual host configuration:
    ubuntu@ubuntu:~$ a2query -s
    example.com (enabled by site administrator)
    
  2. Check the virtual host configuration for any syntax errors:
    ubuntu@ubuntu:~$ sudo apache2ctl -t
    Syntax OK
    
  3. On Nginx, use the following command:
    ubuntu@ubuntu:~$ sudo nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  4. Check the virtual host's details and other Apache configurations:
    $ sudo apache2ctl -S
    
  5. Make sure your virtual host IP and port configuration matches the one defined with NamedVirtualHost.
  6. Check DocumentRoot - does it point to proper files?
    • On Apache:
      <VirtualHost *:80>
       DocumentRoot /var/www/html
      <VirtualHost>
      
    • On Nginx:
      server {
       root /usr/share/nginx/html;
      }
      
  7. Crosscheck your ServerName and ServerAlias variables - do they match your domain name?
    • On Apache, these settings should look similar to this:
      <VirtualHost *:80>
       ServerName example.com
       ServerAlias www.example.com
      </virtualHost>
      
    • On Nginx, the ServerName is defined as this:
      server {
       server_name example.com www.example.com;
      }
      
Access denied or forbidden errors

Check directory permissions for the virtual host root directory. Are they accessible to the web server? Check the web server user and group (commonly www-data) have ready permissions. If required, you can set permissions with chown and chmod commands.

ubuntu@ubuntu:~$ ls -l /var/www/
drwxr-x--- 3 ubuntu www-data 4096 Aug 4 23:00 example.com
drwxr-xr-x 2 ubuntu www-data 4096 Aug 2 23:04 public_html

Secondly, make sure that you have properly set directory permissions in the virtual host configuration. Are they restricting file access?

Use the following commands to set directory permissions in the virtual host configuration:

<Directory /var/www/>
 AllowOverride None
 Order Deny,Allow
 Deny from all
</Directory>
Apache downloads .php files

Make sure that the mod_php module is installed and enabled:

ubuntu@ubuntu:~$ ls -l /etc/apache2/mods-available | grep php
-rw-r--r-- 1 root root 897 Jul 2 21:26 php7.0.conf
-rw-r--r-- 1 root root 59 Jul 2 21:26 php7.0.load

ubuntu@ubuntu:~$ a2query -m | grep php
php7.0 (enabled by maintainer script)