Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php on line 6121

Deprecated: Creation of dynamic property ACF::$fields is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields.php on line 138

Deprecated: Creation of dynamic property acf_loop::$loops is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/loop.php on line 28

Deprecated: Creation of dynamic property ACF::$loop is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/loop.php on line 269

Deprecated: Creation of dynamic property ACF::$revisions is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/revisions.php on line 397

Deprecated: Creation of dynamic property acf_validation::$errors is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/validation.php on line 28

Deprecated: Creation of dynamic property ACF::$validation is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/validation.php on line 214

Deprecated: Creation of dynamic property acf_form_customizer::$preview_values is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-customizer.php on line 28

Deprecated: Creation of dynamic property acf_form_customizer::$preview_fields is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-customizer.php on line 29

Deprecated: Creation of dynamic property acf_form_customizer::$preview_errors is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-customizer.php on line 30

Deprecated: Creation of dynamic property ACF::$form_front is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-front.php on line 598

Deprecated: Creation of dynamic property acf_form_widget::$preview_values is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-widget.php on line 34

Deprecated: Creation of dynamic property acf_form_widget::$preview_reference is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-widget.php on line 35

Deprecated: Creation of dynamic property acf_form_widget::$preview_errors is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-widget.php on line 36

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the all-in-one-wp-migration domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php on line 6121

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp_plugin/wp_plugin.php on line 23

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 54

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 1539

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 828

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rocket domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php on line 6121

Deprecated: Creation of dynamic property acf_field_oembed::$width is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-oembed.php on line 31

Deprecated: Creation of dynamic property acf_field_oembed::$height is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-oembed.php on line 32

Deprecated: Creation of dynamic property acf_field_google_map::$default_values is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-google-map.php on line 33

Deprecated: Creation of dynamic property acf_field__group::$have_rows is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-group.php on line 31

Deprecated: Creation of dynamic property acf_field_clone::$cloning is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-clone.php on line 34

Deprecated: Creation of dynamic property acf_field_clone::$have_rows is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-clone.php on line 35

Deprecated: Creation of dynamic property jh_acf_field_table::$settings is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-table-field/class-jh-acf-field-table.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896
{"id":24603,"date":"2016-01-08T12:59:05","date_gmt":"2016-01-08T10:59:05","guid":{"rendered":"https:\/\/upcloud.com\/community\/tutorials\/haproxy-load-balancer-centos"},"modified":"2016-01-08T12:59:05","modified_gmt":"2016-01-08T10:59:05","slug":"haproxy-load-balancer-centos","status":"publish","type":"tutorial","link":"https:\/\/studiogo.tech\/upcloudold\/tutorial\/haproxy-load-balancer-centos\/","title":{"rendered":"How to install HAProxy load balancer on CentOS"},"content":{"rendered":"\n
\n

Try this guide out on UpCloud with our free trial! Get started<\/a><\/h3>\n<\/div>\n\n\n\n

Load balancing is a common solution for distributing\u00a0web applications horizontally across multiple hosts while providing the users with a single point of access to the service.\u00a0HAProxy<\/a> is one of the most popular\u00a0open-source load-balancing software, which also offers high availability and proxy functionality.<\/p>\n\n\n\n

\n
Try UpCloud for free!<\/a><\/div>\n<\/div>\n\n\n\n

HAProxy aims to optimise resource usage, maximise throughput, minimise response time, and avoid overloading any single resource. It is available for installation on many Linux distributions like CentOS 8 in this guide, but also on Debian 8<\/a> and Ubuntu 16<\/a> systems.<\/p>\n\n\n\n

\"HAproxy<\/figure>\n\n\n\n

HAProxy is particularly suited for very high-traffic websites and is therefore often used to improve web service reliability and performance for multi-server configurations. This guide lays out the steps for setting up HAProxy as a load balancer on CentOS 8 to its own cloud host which then directs the traffic to your web servers.<\/p>\n\n\n\n

As a pre-requirement for the best results, you should have a minimum of two web servers and a server for the load balancer. The web servers need to be running at least the basic web service such as nginx<\/em> or httpd<\/em> to test out the load balancing between them.<\/p>\n\n\n\n

Installing HAProxy CentOS 8<\/h2>\n\n\n\n

As a fast-developing open-source application HAProxy available for installation in the CentOS default repositories might not be the latest release. To find out what version number is being offered through the official channels enter the following command.<\/p>\n\n\n\n

sudo yum info haproxy<\/pre>\n\n\n\n

HAProxy has always three active stable versions of the releases, two of the latest versions in development plus a third older version that is still receiving critical updates. You can always check the currently newest stable version listed on the HAProxy website and then decide which version you wish to go with.<\/p>\n\n\n\n

In this guide, we will be installing the currently latest stable version of 2.0, which was not yet available in the standard repositories. Instead, you will need to install it from the source. But first, check that you have the prerequisites to download and compile the program.<\/p>\n\n\n\n

sudo yum install gcc pcre-devel tar make -y<\/pre>\n\n\n\n

Download the source code with the command below. You can check if there is a newer version available on the HAProxy download page<\/a>.<\/p>\n\n\n\n

wget http:\/\/www.haproxy.org\/download\/2.0\/src\/haproxy-2.0.7.tar.gz -O ~\/haproxy.tar.gz<\/pre>\n\n\n\n

Once the download is complete, extract the files using the command below.<\/p>\n\n\n\n

tar xzvf ~\/haproxy.tar.gz -C ~\/<\/pre>\n\n\n\n

Change into the extracted source directory.<\/p>\n\n\n\n

cd ~\/haproxy-2.0.7<\/pre>\n\n\n\n

Then compile the program for your system.<\/p>\n\n\n\n

make TARGET=linux-glibc<\/pre>\n\n\n\n

And finally, install HAProxy itself.<\/p>\n\n\n\n

sudo make install<\/pre>\n\n\n\n

With that done, HAProxy is now installed but requires some additional steps to get it operational. Continue below with setting up the software and services.<\/p>\n\n\n\n

Setting up HAProxy for your server<\/h2>\n\n\n\n

Next, add the following directories and the statistics file for HAProxy records.<\/p>\n\n\n\n

sudo mkdir -p \/etc\/haproxy\nsudo mkdir -p \/var\/lib\/haproxy \nsudo touch \/var\/lib\/haproxy\/stats<\/pre>\n\n\n\n

Create a symbolic link for the binary to allow you to run HAProxy commands as a normal user.<\/p>\n\n\n\n

sudo ln -s \/usr\/local\/sbin\/haproxy \/usr\/sbin\/haproxy<\/pre>\n\n\n\n

If you want to add the proxy as a service to the system, copy the haproxy.init<\/tt> file from the examples to your \/etc\/init.d<\/tt> directory. Change the file permissions to make the script executable and then reload the systemd daemon.<\/p>\n\n\n\n

sudo cp ~\/haproxy-2.0.7\/examples\/haproxy.init \/etc\/init.d\/haproxy\nsudo chmod 755 \/etc\/init.d\/haproxy\nsudo systemctl daemon-reload<\/pre>\n\n\n\n

You will also need to enable the service to allow it to restart automatically at system boot-up.<\/p>\n\n\n\n

sudo chkconfig haproxy on<\/pre>\n\n\n\n

For general usage, it is also recommended to add a new user for HAProxy to be run under.<\/p>\n\n\n\n

sudo useradd -r haproxy<\/pre>\n\n\n\n

Afterwards, you can double-check the installed version number with the following command.<\/p>\n\n\n\n

haproxy -v<\/pre>\n\n\n\n
HA-Proxy version 2.0.7 2019\/09\/27 - https:\/\/haproxy.org\/<\/pre>\n\n\n\n

In this case, the version should be 2.0.7 as shown in the example output above.<\/p>\n\n\n\n

Lastly, the firewall on CentOS 8 is quite restrictive for this project by default. Use the following commands to allow the required services and reload the firewall.<\/p>\n\n\n\n

sudo firewall-cmd --permanent --zone=public --add-service=http\nsudo firewall-cmd --permanent --zone=public --add-port=8181\/tcp\nsudo firewall-cmd --reload<\/pre>\n\n\n\n

Configuring the load balancer<\/h2>\n\n\n\n

Setting up an HAProxy load balancer is a quite straightforward process. Basically, all you need to do is tell HAProxy what kind of connections it should be listening for and where the connections should be relayed to.<\/p>\n\n\n\n

This is done by creating a configuration file \/etc\/haproxy\/haproxy.cfg<\/tt> with the defining settings. You can read about the configuration options on the HAProxy documentation page<\/a> if you wish to find out more.<\/p>\n\n\n\n

Load balancing at layer 4<\/h2>\n\n\n\n

Start off with a basic setup. Create a new configuration file, for example, using vi<\/tt> with the command underneath.<\/p>\n\n\n\n

sudo vi \/etc\/haproxy\/haproxy.cfg<\/pre>\n\n\n\n

Add the following sections to the file. Replace the server_name<\/tt><\/span> with whatever you want to call your servers on the statistics page and the private_ip<\/tt><\/span> with the private IPs for the servers you wish to direct the web traffic. You can check the private IPs at your UpCloud control panel<\/a> and Private network<\/em> tab under the Network<\/em> menu.<\/p>\n\n\n\n

global\n   log \/dev\/log local0\n   log \/dev\/log local1 notice\n   chroot \/var\/lib\/haproxy\n   stats timeout 30s\n   user haproxy\n   group haproxy\n   daemon\n\ndefaults\n   log global\n   mode tcp\n   option httplog\n   option dontlognull\n   timeout connect 5000\n   timeout client 50000\n   timeout server 50000\n\nfrontend http_front\n   bind *:80\n   stats uri \/haproxy?stats\n   default_backend http_back\n\nbackend http_back\n   balance roundrobin\n   server server_name1<\/span> private_ip1<\/span>:80 check\n   server server_name2<\/span> private_ip2<\/span>:80 check<\/pre>\n\n\n\n

This defines a layer 4 load balancer with a front-end name http_front<\/tt> listening to the port number 80<\/tt>, which then directs the traffic to the default backend named http_back<\/tt>. The additional stats URI \/haproxy?stats<\/tt> enables the statistics page at that specified address.<\/p>\n\n\n\n

Different load balancing algorithms<\/h2>\n\n\n\n

Configuring the servers in the backend section allows HAProxy to use these servers for load balancing according to the roundrobin<\/tt> algorithm whenever available.<\/p>\n\n\n\n

The balancing algorithms are used to decide which server at the backend each connection is transferred to. Some of the useful options include the following:<\/p>\n\n\n\n