- Apache Web Server Windows
- Apache Web Server Tutorial
- Apache Web Server Linux
- Apache Web Server Linux Download
- Apache Web Server Versions
Apache HTTP Server 2.4.46 (httpd): 2.4.46 is the latest available version 2020-08-07.
In the previous tutorial I showed how to install wordpress on Digital Ocean droplet andconfigure it to use Digital Ocean Managed Database service, we used a very simpleand quickly setup, here we will explore more options to run Wordpress usingthe Apache web server.
Apache is a popular Web Server software, it is used to host many web applicationsaround the world. Apache has a unique feature that enables it to run PHP scriptsin its own process without calling an external server such as PHP-FPM, howeverwe will find out that, this feature is not very good at all when it comes to servingmany simultaneous requests as we will see in the next sections.
Here we assume you already have the previous Wordpress site createdin my last tutorial which can be found here.
Clone the github repository. Put it in the apache web server root. Go to app/config/config.php file and set the base url. Go to app/config/database.php and set the mysql credentials; That's it time to enjoy the framework! Apache HTTP Server What is it? - The Apache HTTP Server is a powerful and flexible HTTP/1.1 compliant web server. Originally designed as a replacement for the NCSA HTTP Server, it has grown to be the most popular web server on the Internet.
In this tutorial we will:
Create a droplet and install locustio for load testing our Wordpress site.
Explore the available modules for serving client requests in Apache server.
Run load tests for Wordpress site using different apache configurationsand specify which one is the best for high performance.
At the end of this tutorial you are expected to configure apache web serverfor hosting Wordpress site and achieve the best performance.
Using the Digital Ocean dashboard create a new droplet with the name wp-locustand 4 GB RAM and 2 vCPUs to use it for running load tests on Wordpress site.
locustio is a load testing tool written in Python, it can beused to load test any system as long as you have the right client setup in Python.
locust comes with built-in HTTP client that can be used for load testing web serversand applications, here we will install locustio and run a simple load test using it.
First execute these commands to install locustio on the new droplet
The first two commands updates the package repositories and installs pip3
which is used to install python 3 packages on the system.
The third command installs locustio
python package on the system so wecan use it to run the load tests.
locust uses python code to define user behavior and runs multiple users tomake requests to the web server, this makes it easier than other tools which useXML files to define user behavior.
We will create a simple locust file named locustfile.py
with this content
In the previous file we have two classes, the first one is called WordPressTasks
which represents tasks performed by locust users, locust uses tasks(methods decorated with @task) in this class to make HTTP requests to the server,here we have two tasks the first one is a request to Wordpress main page and thesecond one is a request to Wordpress login page.
The other class is WordPressUser
and represents users in locust, here we specifytask_set class attribute that defines the class used to create tasks, alsothe host class attribute holds the URL to the server and the wait_time specifieshow many seconds users wait before making another request.
Now to run locust testing we use this command
- –no-web: runs locust without the web interface.
- -c 500: Simulate 500 users
- -r 50: This rate specifies how many users are added per second.
- -t2m: This means run the test for 2 minutes then exit.
- –only-summary: This means only show summary of tests when they end.
- –csv=500.50: This option tells locust to store results in CSV files,with names that start with “500.50”
After you execute the command wait two minutes until it exits and check the resultsas shown in this image.
From the summary in the previous image we can see the following:
- The total average response time is 9528 ms which is 9.5 seconds.
- The total number of requests is 2626 requests.
- The number of requests per second is 21.72 req/s
- There were 16 failed requests, because of Connection Reset by the server.
From the previous summary we can see that the result is not acceptable at all, 9.5seconds to just load the HTML part of the page (CSS and JavaScript files are notloaded yet) is not okay at all and our server will just go down once we havemany users accessing our site, to improve performance we must understand howapache handles new requests in the next section.
Apache uses Multi-Process Modules (MPMs) to handle new requests, each modulespecifies how a new request is served, some modules uses multiple processesto handle requests (prefork module), some others use multi-threading (workermodule) and others use events (event module) to handle new requests.
To tell which MPM module you are currently using issue this command
You will get the result prefork for the Wordpress droplet.
Apache prefork MPM
This module preforks a number of apache processes to serve client requests, eachrequest is served in its own process, and when more requests arrive than the numberof processes, apache will create new processes to handle incoming requests.
This module is considered bad for performance in modern web applications, usinga separate process for each request is very bad and requires a lot of server’sresources to serve requests, we saw the bad results in the previous section.
We will explore another MPM called worker in the next section.
Apache worker MPM
This module uses a thread to handle each new request, using threadsto handle requests causes little overhead compared to processes butwith this module we cannot use apache’s built-in PHP module to execute PHPscripts because this module requires that each request is served in a separateprocess as executing PHP scripts is not thread-safe which means that thescript may modify data shared by multiple threads without using any methodto synchronize access. To solve this we must use an external PHP serversuch as PHP-FPM
.
Installing PHP-FPM
You can install php-fpm with this command
This will install the package php-fpm and start a PHP server and createa socket in /run/php/php7.2-fpm.sock
which can be used to communicatewith the PHP server from apache on the same host.
Use PHP-FPM in apache
In order to make apache use php-fpm to execute PHP scripts instead ofits own php module, we need to execute these commands
The first one enables the proxy_fcgi
and setenvif
modules which areneeded for apache to communicate with PHP-FPM and the second one enablesPHP-FPM configuration to use php-fpm with php files.
Untill now apache still uses its own php module and the prefork module, touse the worker module we must first disable prefork and then enable worker,with these commands:
The first command disables the prefork MPM, we also disabled php7.2 becauseit relies on prefork to work and we cannot disable prefork and keep php7.2 enabled.
The second command enables the worker MPM and the last one restarts apache serverto make changes take effect.
Apache event MPM
The event MPM was made stable only in apache version 2.4 and it offers significantperformance boost (as we will see in the next section), this new MPM makes useof new kernel features to handle many requests concurrently and with very littleresource consumption, all of this was made available thanks to the poll and epollsystem calls, which enable user space applications to monitor multiple filedescriptors and get notified when they are available for reading or writing.
The event MPM uses threads to serve requests, however it does not use a singlethread per request, it uses a thread that listens to incoming connections and whena new connection is accepted it adds it to the epoll list and other idle threadwill take the new connection from the list and serve it.
Apache Web Server Windows
The event MPM also needs to use PHP-FPM server to execute PHP scripts, becauseit serves requests in threads not in separate processes.
To enable the event MPM use these commands:
We first disable the worker MPM and then enable event MPM, notice thatonly a single MPM can be enabled at a time so we first must disable the enabledMPM then enable the new one.
Now after we learned about 3 of the most used apache MPMs we will restart the testsusing these three configurations and check which one is better and gives lowerresponse time and less errors.
To do these tests enable the right MPM in apache and execute this command
We described this command previously, the following two images show the resultsfor worker
and event
MPMs (results for prefork MPM were shown in the first section).
Results for worker MPM (average response time is 8.3 seconds, 7 errors, 22.72 req/s)
Apache Web Server Tutorial
Results for event MPM (average response time is 2.1 seconds, 5 errors, 50.94 req/s)
From the previous tests we can clearly see that the event MPM offers a very bigperformance boost compared to prefork and worker MPMs, worker MPM is slightlybetter than prefork MPM.
We can also see that the number of requests per second was a lot higher with eventMPM which means it can handle more simultaneous users.
In this tutorial we learned how to use the right modules in apache to hostWordpress sites, we also used locustio to load tests Wordpress under differentapache MPM modules.
Apache Web Server Linux
Our tests show that prefork and worker modules are not suitable at all forhigh performance applications, and the use of event MPM improves the performancea lot when hosting Wordpress sites.
Apache Web Server Linux Download
In the next tutorials we will compare apache with nginx server and alsodiscover how to host Wordpress sites on multiple Digital Ocean dropletsand load balance them using Digital Ocean load balancer service.
I hope you find the content useful for any comments or questions you can contact meon my email address mohsen47@hotmail.co.uk
Apache Web Server Versions
Stay tuned for more articles. :) :)