Installing and setting up your own Wordpress site on NGinx
published onThis article will be very similar to the Apache version. Most of it will be copied from Installing and setting up your own Wordpress site on Apache.
Whilst I prefer Joomla to Wordpress there are many more people using Wordpress online and so it only seems right to add an article explaining how to install the latest version of Wordpress (version 6.0 at the time of writing) using PHP 8.1, MariaDB and Redis cache.
Start by installing PHP 8.1
The first thing I did when setting up my server for the Wordpress installation is to install PHP 8.1. This speeds up executing PHP scripts as it includes a JIT (or just in time) compiler which PHP 7.4 and below didn't have.
To install PHP, and a few other tools, that might be useful later enter the following commands:
sudo add-apt-repository -y ppa:ondrej/phpsudo apt updatesudo apt install php8.1 php8.1-{cli, fpm, mysql, gd, soap, mbstring, bcmath, common, xml, curl, imagick, bz2, intl, zip, ldap, opcache, exif, imagick, gd, mcrypt, ssh2}sudo apt install zip unzip ghostscript imagemagick
Now edit the PHP pool configuration file with sudo nano /etc/php/8.1/fpm/pool.d/www.confand update statements in the file to look like the below. You may need to uncomment (remove the ;) on some lines.
listen = /run/php/php8.1-fpm.socklisten.owner = www-datalisten.mode = 0660
Once done edit the php.ini file by running sudo nano /etc/php/8.1/fpm/php.ini and updating the following lines to look like the ones below. Make sure to add any lines below that you can't find in the file.
expose_php = offmax_execution_time = 30max_input_time = 60memory_limit = 512Mopcache.enable=1opcache.jit_buffer_size=100Mopcache.jit=1255opcache.memory_consumption=128opcache.max_accelerated_files=10000opcache.revalidate_freq=200
Now restart PHP 8.1 by running sudo systemctl restart php8.1-fpm.
Now install Redis cache
Installing the Redis cache will help to speed up your site by storing some of the data in memory rather than it needing to be read from disk each time.
Enter the following commands to install it
sudo add-apt-repository ppa:redislabs/redissudo apt-get updatesudo apt-get install redis-server php-redissudo systemctl enable redis-server.service
Make a few changes to the configuration by running sudo nano /etc/redis/redis.conf and editing lines similar to the below to read as follows
bind 127.0.0.1 ::1supervised systemdmaxmemory 256mbmaxmemory-policy allkeys-lru
Lastly, reload the Redis service
sudo systemctl restart redis.service
Install and setup the MariaDB database
I like to install extra software that makes it easy for me to backup my MariaDB databases. Running the following command will achieve this
sudo apt-get install mariadb-server mariadb-backup
Once installed it is important to run sudo mysql_secure_installation to secure the installation. Remember that, initially, their is no MariaDB root password!
Lastly I run sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf to make two changes to the configuration file
bind-address = 127.0.0.1local-infile = 0
then I restart the service with sudo systemctl restart mariadb.service.
Now I can set up a new database for Wordpress to use. This can be done by running sudo mysql -u root -p and entering the root password you created just above.
Now enter the following lines, making sure to substitute your own database name, user name and password, before entering exit to return to the command prompt.
CREATE DATABASE [wordpress_db] charset=utf8mb4;CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'my_password';GRANT ALL PRIVILEGES ON wordpress_db.* to 'wordpress_user'@'localhost';FLUSH PRIVILEGES;
Installing the NGinx web server
In this article we are using the NGinx web server. These commands presume that you are using the Uncomplicated Firewall (UFW). If not then substitute the relevant commands with the ones for the firewall that you are using.
First add the repository, so that the packages installed will be up-to-date ones, with sudo add-apt-repository ppa:ondrej/nginx-mainline.
Before updating the package manager add the NGinx source repository by running sudo nano /etc/apt/sources.list.d/ondrej-ubuntu-nginx-mainline-*.list and uncommenting the deb-src line.
Now run the following commands to get NGinx installed. These commands will also disable the default site.
sudo apt updatesudo apt install nginx-core nginx-common nginx nginx-full libgeoip-dev libpcre3-devsudo unlink /etc/nginx/sites-enabled/defaultsudo ufw allow 80sudo ufw allow 443sudo ufw enable
Downloading and installing the latest version of Wordpress
The commands you will need to run to download and save the Wordpress files in the correct place can be found below. Make sure to substitute [domain] with your own domain name.
cd ~wget -c http://wordpress.org/latest.tar.gztar -xzvf latest.tar.gzsudo mkdir /var/www/[domain]sudo cp -R wordpress/* /var/www/[domain]
Now, edit the sample configuration file to add memory limits by running sudo nano /var/www/[domain]/wp-config-sample.php and adding the following two lines near the top of the file.
define('WP_MEMORY_LIMIT', '512M');define( 'WP_MAX_MEMORY_LIMIT', '512M' );
Finally, set up ownership and file permssions with
sudo chown -R www-data:www-data /var/www/[domain]sudo find /var/www/[domain]/ -type d -exec chmod 755 {} \;sudo find /var/www/[domain]/ -type f -exec chmod 644 {} \;
Create NGinx's server block
Run sudo nano /etc/ngnix/sites-available/[domain], making sure to substitute [domain] for your own domain, and add the following lines to the file
server { listen 80; root /var/www/[domain]; index index.html index.htm index.php; server_name [domain]; error_log /var/log/nginx/mysite.com_error.log; access_log /var/log/nginx/mysite.com_access.log; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } location ~ /\.ht { deny all; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; allow all; } location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { expires max; log_not_found off; }}
and lastly run the following two commands
sudo ln -s /etc/nginx/sites-available/[domain] /etc/nginx/sites-enabled/sudo systemctl reload nginx
Make your site secure with a certificate
Run the following commands to provide your site with a free SSL / TLS certificate. This will make sure that it can be accessed using https and should set up the necessary redirects in case anybody tries to visit the non - secure version.
sudo apt install snapdsudo snap install core; sudo snap refresh coresudo snap install --classic certbotsudo ln -s /snap/bin/certbot /usr/bin/certbotsudo certbot -d [domain] --nginx
Setup Wordpress
The final step is to visit your domain name in your browser of choice and click through the setup screens you see displayed. Remember to enter the same database details that you used earlier in order that Wordpress can connect to your database.
Once you have logged in to the Wordpress backend make sure to install the Redis Obect Cache plugin and activate it.
Final thoughts
This article, like the Apache one, covers the basics in getting a fast, performant, version of Wordpress installed. You will probably want to develop the server block to meet your own needs.
Whilst this is a good start to a fast website remember that the server can only help you so much. If you have a slow site then the server will not be able to solve this for you. When creating your site try to keep it as lean as possible.
