Websites hosted on an Apache server can slow down for various reasons, such as high resource usage by other processes, outdated software, or misconfigurations. In this tutorial, we will cover how to use various commands in Ubuntu to identify the processes that are causing the slowdowns and how to resolve the issue.
Identify High Resource Usage
One common cause of slow websites is high resource usage by other processes on the server. To see which processes are using the most resources, run the following command in the terminal:
ps aux
The ps aux
command will display a list of all running processes and their resource usage, including CPU, memory, and disk I/O. Look for any processes that are using a high percentage of CPU or memory.
Another useful command for identifying high resource usage is top
, which provides a real-time view of the server’s resource usage:
top
The top
command will display a list of the processes that are using the most resources at the current moment. You can sort the list by CPU or memory usage by pressing the Shift + p
or Shift + m
keys, respectively.
Determine the Number of Cores
To accurately interpret the results of the top
command, it is important to know the number of cores on the server. To find the number of cores, run the following command:
nproc --all
Find the Location and Command of the Process
Once you have identified the process that is causing the slowdowns, you need to determine where it is located on the system and what command or file called it to run.
To find the location of the process, use the lsof
command and the process ID (PID) from top
:
lsof -p [PID]
This will display a list of all the open files associated with the process, including the executable file and any configuration files.
To find the command or file that started the process, use the ps
command with the process ID:
ps -p [PID] -o cmd
Change Process Priorities with renice
In some cases, you may be able to resolve the slowdowns by adjusting the priority of the processes that are using high resources. To adjust the priority of a process, use the renice
command and the process ID:
renice [priority] [PID]
The priority value should be between -20 and 19, with a lower number indicating a higher priority. 0 is base priority. 19 is highest. For example, to give a process a higher priority, you could use the following command:
renice -10 [PID]
Force Stop the Process with kill
If adjusting the priority of the process does not resolve the slowdowns, you may need to force stop the process. To force stop a process, use the kill
command and the process ID:
kill -9 [PID]
Note: Before force stopping a process, make sure to backup any important data associated with the process and verify that the process is not a critical component of the system or another website.