In the next article we are going to take a look at how we can install WordPress with Nginx on Ubuntu 20.04. This CMS is one of the most widely used open source content management systems. It powers around 60 million websites. It is written in PHP and uses MariaDB / MySQL as a database to store information.
In the following lines we are going to see how to install WordPress locally with Nginx on Ubuntu 20.04. For this reason, before continuing it will be necessary have the LEMP software stack installed on Ubuntu 20.04 to get started.
Install WordPress with Nginx on Ubuntu 20.04
Install PHP extensions
The following extensions are required for WordPress to run on Ubuntu 20.04. To install them we will only have to open a terminal (Ctrl + Alt + T) and execute:
sudo apt update && sudo apt install php-dom php-simplexml php-ssh2 php-xml php-xmlreader php-curl php-exif php-ftp php-gd php-iconv php-imagick php-json php-mbstring php-posix php-sockets php-tokenizer
Create Nginx Server Block for WordPress
We will create an Nginx server block for the WordPress installation. This server block requires a domain name, port number, document root, registry location, etc.. For this example, the data that I am going to use is the following. Let each user adapt them according to their needs:
- Domain name: www.wordpress.local
- Document root directory: /sites/www.wordpress.local/public_html/
- logs: /sites/www.wordpress.local/logs/
Let's start creating a server block configuration file in the directory /etc/nginx/conf.d with the command:
sudo vim /etc/nginx/conf.d/www.wordpress.local.conf
Inside the file we will place the following content:
server { server_name www.wordpress.local; root /sites/www.wordpress.local/public_html/; index index.html index.php; access_log /sites/www.wordpress.local/logs/access.log; error_log /sites/www.wordpress.local/logs/error.log; # No permitir que las páginas se representen en un iframe en dominios externos add_header X-Frame-Options "SAMEORIGIN"; # Prevención MIME add_header X-Content-Type-Options "nosniff"; # Habilitar el filtro de secuencias de comandos entre sitios en los navegadores compatibles add_header X-Xss-Protection "1; mode=block"; # Evitar el acceso a archivos ocultos location ~* /\.(?!well-known\/) { deny all; } # Evitar el acceso a ciertas extensiones de archivo location ~\.(ini|log|conf)$ { deny all; } # Habilitar enlaces permanentes de WordPress location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
We save the file and exit. Now we are going to create the document root directory and the records directory using the commands:
sudo mkdir -p /sites/www.wordpress.local/public_html/ sudo mkdir -p /sites/www.wordpress.local/logs/
We continue checking the Nginx configuration files:
sudo nginx -t
A message like the one in the previous screenshot will confirm that the Nginx server configuration is correct. We end up restarting the service:
sudo systemctl restart nginx.service
Create the database for WordPress
Let's log into MariaDB / MySQL:
sudo mysql -u root -p
Now, we create the database for WordPress:
CREATE DATABASE wordpress;
The following will be create a user:
CREATE USER 'wpusuario'@'localhost' IDENTIFIED BY '123password';
We continue giving permission to the created user to access the database:
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpusuario'@'localhost';
And we can salir:
quit
Download WordPress
We download the latest version of WordPress de WordPress.org with wget:
wget http://wordpress.org/latest.tar.gz
Now let's extract the WordPress package with the command tar:
tar -zxvf latest.tar.gz
The following will be move WordPress files to document root:
sudo mv wordpress/* /sites/www.wordpress.local/public_html/
We continue to change the property so that Nginx can write files to that document root:
sudo chown -R www-data:www-data /sites/www.wordpress.local/public_html/ sudo chown -R www-data:www-data /sites/www.wordpress.local/logs/
Now let's create a host entry for the domain (in this example www.wordpress.local) in the File / Etc / hosts, in case our environment does not have a DNS server for name resolution:
sudo vim /etc/hosts
Inside the file, we are going to add an entry as shown below. The IP used is that of my local computer.
Install WordPress
Following the data in this example, we are going to open web browser and visit url:
http://www.wordpress.local
This will lead us to WordPress installation wizard.
We will have to write the database details to allow WordPress to connect to it. It will be the data from the database created previously
If the connection is successful, we will see a success message on a new screen. To continue you just have to click on Run the installation.
On the next screen we will have to write the site title, WordPress admin user, password and email address. We will go to the next screen by clicking Install WordPress.
If all goes well, the WordPress installation is now complete. We can now click on Access to go to the WordPress Administrator (Backend).
And from there we can start developing our site:
Set the maximum file upload size
By default, PHP does not allow file uploads greater than 2MB. To allow larger file uploads through the WordPress web interface, we will have to configure upload_max_filesize and post_max_size in php.ini.
sudo vim /etc/php/7.4/fpm/php.ini
Here we go to attempting upload_max_filesize y change upload size to 256M, if it is what you need:
upload_max_filesize = 256M
We will also attempting post_max_size and we will change the upload size according to our needs:
post_max_size = 256M
To finish we will add the main module client_max_body_size in the Nginx server configuration file.
sudo vim /etc/nginx/nginx.conf
The directive can be added to the HTTP block (for all sites), particular server block or location context.
client_max_body_size 256M;
We save the file and exit. We end up restarting the services:
sudo systemctl restart php7.4-fpm.service sudo systemctl restart nginx.service
And with this we will have WordPress installed locally on Ubuntu 20.04.
It does not work 🙁
Hello. At what point did the installation fail?
How can I configure Nginx to access from an external machine from the same local network?
When applying the indicated configuration and trying to enter by name access to the default of Nginx.