Product used: RS 1000 G9.5 a1 12M
from 9.81€ / month | go to product >
Welcome to this tutorial on optimizing and installing WordPress on a LEMP stack. LEMP stands for Linux, Nginx, MariaDB, and PHP, and it's a powerful combination that can help you run WordPress faster and more efficiently.
In this tutorial, we'll cover the steps required to set up a LEMP stack on your server, install and configure WordPress, and optimize its performance using various tools and techniques. Whether you're a beginner or an experienced user, you'll find valuable information that can help you improve the speed, security, and stability of your WordPress instance.
So, let's get started and learn how to install and optimize WordPress on a LEMP stack!
To install and optimize WordPress on a LEMP stack, you'll need to make sure your system meets the following requirements:
By meeting these requirements, you'll be able to successfully install and optimize WordPress on a LEMP stack.
At the time of the creation of this tutorial (April 2023), the recommended product to use is the RS 1000 G9.5 a1 12M. Alternatively, a VPS product can be used. Existing customers can add the product easily and quickly.
After you have undergone the provisioning of the server and successfully logged in for the first time with the username root
and the password sent to you by email, the first step is to update the basic configuration of the server.
passwd
.apt-get update && apt-get upgrade -y
.sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo apt install mariadb-server
sudo systemctl start mariadb
sudo mariadb_secure_installation
sudo apt install php-fpm php-mariadb
listen.group = www-datalisten.owner = www-data
sudo systemctl restart php8.2-fpm
Note that the specific commands and version numbers may vary depending on your Linux distribution and version. Also, be sure to follow best practices for securing your server and applications.
Download WordPress:
wget https://wordpress.org/latest.zip
sudo tar -zxvf ~/Downloads/latest.tar.gz -C /var/www/html
sudo mariadb
CREATE DATABASE wordpress
;CREATE USER 'wordpressuser'@'localhost' IDENTIFIED WITH mariadb_native_password BY 'password';
sudo mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo nano /var/www/html/wp-config.php
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wordpressuser' ); define( 'DB_PASSWORD', 'password' );
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html/ -type f -exec chmod 644 {} \;
and sudo find /var/www/html/ -type d -exec chmod 755 {} \;
Configure Nginx to use caching, which can significantly reduce the number of requests to PHP and the database. And use the gzip module to compress responses before sending them to clients.
Use the fastcgi_cache module to cache PHP responses.
/etc/nginx/nginx.conf
or /etc/nginx/conf.d/default.conf
.fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_valid 200 60m; fastcgi_cache_bypass $http_pragma; fastcgi_cache_revalidate $http_cache_control; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500;
/var/cache/nginx
with the path where you want to store your cache files.Use cache-control:
expires 1d;
add_header Cache-Control "public, max-age=86400, immutable";
sudo service nginx reload
to reload Nginx with the new configuration.Activate gzip compression
/etc/nginx/nginx.conf
or /etc/nginx/conf.d/default.conf
.gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1;
Activate opcache:
zend_extension=opcache
'opcache.enable=1 opcache.enable_cli=1 opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=10000 opcache.validate_timestamps=0 zend_extension points to the opcache extension file. opcache.enable enables opcache. opcache.enable_cli enables opcache for command line interface (CLI) scripts. opcache.memory_consumption sets the amount of memory that opcache can use. opcache.interned_strings_buffer sets the amount of memory that opcache can use for string internment. opcache.max_accelerated_files sets the maximum number of PHP files that can be cached by opcache. opcache.validate_timestamps disables the timestamp validation for cached files.'
Activate memcache:
sudo apt-get install php-memcached
.sudo systemctl restart php7.4-fpm.service
.You have now activated opcache and memcache in your LEMP stack.
Tune the MariaDB configuration file:
' > slow_query_log=1 slow_query_log_file=/var/log/mysql/mariadb-slow.log long_query_time=2
key_buffer_size=128M
sort_buffer_size=4M read_buffer_size=1M read_rnd_buffer_size=2M join_buffer_size=8M innodb_buffer_pool_size=512M
query_cache_type=1
query_cache_limit=4M query_cache_size=64M innodb_flush_method=O_DIRECT innodb_file_per_table=1
Optimize tables: Use the mysqlcheck command to optimize all tables in all databases:
sudo mysqlcheck --optimize --all-databases
sudo systemctl restart mariadb.service
Use a CDN to serve static assets, such as images, videos, and JavaScript files, from a network of servers located around the world. This can reduce the load on your server and improve the overall performance of your website.
Combine and minify CSS and JavaScript files to reduce the number of HTTP requests. Use sprites to combine small images into a single image to reduce the number of HTTP requests. Use lazy loading to load images and videos only when they are needed.
Regularly optimize the database to reduce the size of tables and improve performance. Use a caching plugin such as WP Super Cache or W3 Total Cache to cache database queries. By following these tips, you can improve the performance of your LEMP stack and provide a faster and more responsive experience for your users.
crontab -e
to open your user's Crontab file.*/5 * * * * sudo -u www-data php /var/www/html/wp-cron.php > /dev/null 2>&1
That's it! The WordPress scheduled tasks will now be executed every 5 minutes using the system's Crontab instead of relying on web requests.
Copyright (c) 2021 netcup
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
By making a contribution to this project, I certify that:
The contribution was created in whole or in part by me and I have the right to submit it under the licence indicated in the file; or
The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate licence and I have the right under that licence to submit that work with modifications, whether created in whole or in part by me, under the same licence (unless I am permitted to submit under a different licence), as indicated in the file; or
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the licence(s) involved.