Troubleshoot common issues with Apache websites

更新时间:
复制 MD 格式

This topic describes how to fix common issues with Apache websites built on Alibaba Cloud ECS instances.

Details

Note

Note:

  • If you perform risky operations, such as modifying instances or data, consider the disaster recovery and fault tolerance capabilities of the instances to ensure data security.

  • If you modify the configurations or data of instances, such as ECS and RDS, create snapshots or enable features such as RDS log backup in advance.

  • If you have granted permissions or submitted security information, such as logon credentials, on the Alibaba Cloud platform, change them promptly.

Select a solution below based on the issue you are experiencing.

Apache website cannot be opened

Check whether Apache has started properly

Log on to the server and run the following command to check whether the server is listening on ports 80 and 443.

netstat -nltp |grep -E '80|443'

Run the following command to check whether the Apache process is running.

ps aux |grep httpd     #The process name is httpd on CentOS/Alinux systems.
ps aux |grep apache    #The process name is apache2 on Ubuntu systems.

Start the Apache service

If Apache is not running, run the following command to start it.

systemctl start httpd    #For CentOS/Alinux systems
systemctl start apache2   #For Ubuntu systems

Check security group and firewall rules

  1. In the ECS console, check whether the security group rules allow inbound traffic on ports 80 and 443.

    1. Log on to the ECS console.

    2. In the navigation pane on the left, choose Instances & Images > Instances.

    3. In the instance list, find the target ECS instance and click the instance name to open the instance details page.

    4. Go to the Security Groups tab and view the inbound rules.

  2. On the server, run the following command to check whether any iptables firewall rules are configured.

iptables -nL

Apache website returns a 4xx or 5xx error

Apache website returns a 403 error

  1. Check the file permissions of the site directory to verify that Apache has permission to read from and write to the site files.

    The default site directory is /var/www/html/.

    Run the following command to view the permissions of the /var/www/html/ directory. A typical permission setting is 755.

    stat /var/www/html/

    Run the following command to view the permissions of the site files in the directory. A typical permission setting is 644.

    ls -l /var/www/html/
  2. Check the Apache configuration file to verify that access to the site directory is not denied.

    • The default configuration files for CentOS/Alinux systems are as follows:

    • /etc/httpd/conf/httpd.conf

    • /etc/httpd/conf.d/

    • The default configuration files for Ubuntu systems are as follows:

    • /etc/apache2/apache2.conf

    • /etc/apache2/sites-enabled/

    Check the <Directory> section in the configuration file to verify whether `Deny from all` is configured.

  3. Check whether the website's home page file exists.

    • For CentOS/Alinux systems:

    • Check the `DirectoryIndex` directive in the httpd.conf file to identify the default home page. The default home page is typically index.html or index.php.

    • For Ubuntu systems:

    • Check the `DirectoryIndex` directive in the /etc/apache2/mods-enabled/dir.conf file to identify the default home page.

Apache website returns a 404 error

  1. Check the vhosts section in the Apache configuration file to verify that the site directory and domain name configurations are correct.

    • The default configuration files for CentOS/Alinux systems are as follows:

    • /etc/httpd/conf/httpd.conf

    • /etc/httpd/conf.d/

    • The default configuration files for Ubuntu systems are as follows:

    • /etc/apache2/apache2.conf

    • /etc/apache2/sites-enabled/

  2. Check whether the program files exist in the site directory.

    • The site directory is specified by the `DocumentRoot` directive in the configuration file.

    • If you use virtual hosts (vhosts) to configure multiple sites, check the `DocumentRoot` directive for the corresponding virtual host.

Apache website returns a 5xx error

  1. Check the Apache error log and analyze the error messages to troubleshoot the issue.

    Run the following command to view the error log.

    less /var/log/httpd/error_log #For CentOS/Alinux systems
    less /var/log/apache2/error.log #For Ubuntu systems
  2. Check whether the `.htaccess` rewrite rule configuration is correct.

  3. Check whether the permissions for the site directory are correct. A typical permission setting is 755.

    The default site directory is /var/www/html/.

    Run the following command to view the permissions of the /var/www/html/ directory.

    stat /var/www/html/

    To find the actual site directory, check the `DocumentRoot` directive within the `VirtualHost` block in the configuration file.

    • The default configuration files for CentOS/Alinux systems are as follows:

    • /etc/httpd/conf/httpd.conf

    • /etc/httpd/conf.d/

    • The default configuration files for Ubuntu systems are as follows:

    • /etc/apache2/apache2.conf

    • /etc/apache2/sites-enabled/

  4. Enable detailed PHP error logging to view PHP program error messages.

    Modify the php.ini configuration file and add the following configurations to enable detailed log output.

     display_errors = Off
     error_reporting = E_ALL | E_STRICT

    Run the following command to restart Apache. Then, access the PHP page to view any errors.

    systemctl restart httpd #For CentOS/Alinux systems
    systemctl restart apache2 #For Ubuntu systems

Troubleshoot slow access to an Apache website

Check server performance

Check server performance metrics, such as public bandwidth, CPU utilization, and disk input/output operations per second (IOPS).

Check the Apache error log for error messages

Run the following command to view the Apache error log.

less /var/log/httpd/error_log     #For CentOS/Alinux systems
less /var/log/apache2/error.log   #For Ubuntu systems

Check if the number of Apache httpd processes has reached the configured maximum

Run the following command to count the number of httpd processes.

ps aux |grep httpd |wc -l      #For CentOS/Alinux systems
ps aux |grep apache2 |wc -l    #For Ubuntu systems

Apache has three Multi-Processing Modules (MPMs) that determine its working mode: prefork, worker, and event. For more information about tuning, see the official Apache documentation:

To check the Apache working mode, run the following command on CentOS/Alinux systems:

httpd -V 

On Ubuntu systems, run the following command:

a2query -M
[root@localhost]# httpd -V        #For CentOS/Alinux systems
Server version: Apache/2.4.6 (CentOS)
Server built:   Mar 24 2022 14:57:57
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork            #Indicates that the prefork mode is used
  threaded:     no
    forked:     yes (variable process count)

[root@localhost]# a2query -M    #For Ubuntu systems
prefork

Fix abnormal operation of Apache, PHP, or MySQL

How to check for abnormal Apache operation

You can restart the Apache service.

systemctl restart httpd #For CentOS/Alinux systems
systemctl restart apache2 #For Ubuntu systems

Examine the Apache error log and analyze the error.

Run the following command to view the Apache error log.

less /var/log/httpd/error_log #For CentOS/Alinux systems
less /var/log/apache2/error.log #For Ubuntu systems

How to check for abnormal PHP operation

1. Enable detailed PHP error messages.

Modify the php.ini configuration file and add the following configurations to enable detailed log output.

  display_errors = Off
  error_reporting =  E_ALL | E_STRICT

Run the following command to restart Apache.

systemctl restart httpd    #For CentOS/Alinux systems
systemctl restart apache2   #For Ubuntu systems

2. Access the PHP page and use the detailed error messages to troubleshoot the issue.

How to check for abnormal MySQL operation

Check the MySQL log and analyze the errors.

Run the following command to view the MySQL log.

less /var/log/mysqld.log #For CentOS/Alinux systems
less /var/log/mysql/error.log #For Ubuntu systems