Skip to main content

Quick Install Wordpress on Ubuntu

This is a guide with codes to quickly setup wordpress / woocommerce on ubuntu for development and production. This guide will use nginx, php-fpm and maria-db

Installing Dependencies

Install the required dependencies for worpress

  • NGINX - Web server
  • PHP - scripting language for web development. We will also need some of its exentsion
  • PHP-FPM - it's primary goal is to handle multiple PHP requests concurrently
  • Maria-DB - relational database management system
sudo apt install php \
php-bcmath \
php-curl \
php-imagick \
php-intl \
php-json \
php-mbstring \
php-mysql \
php-xml \
php-zip \
php-fpm \
mariadb-server \
mariadb-client \
nginx

if apache (another webserver) is installed, remove the same using

Check

apache2 -v

Remove

sudo apt purge apache*
sudo apt autoremove

Download Wordpress

Now we need to download the latest wordpress package and deploy the same in /var/www Note the when unzipping the wordpress package the wordpress folder will be created automatically and so the final path will be /var/www/wordpress

curl https://wordpress.org/latest.tar.gz | sudo tar zx -C /var/www

Provide proper permission to user www-data (server) for the wordpress directory Use this permission for development purpose, but latter on you can tighten the permission for better security. Read More

sudo chown www-data:www-data -R /var/www/wordpress

Setting up Database

We need to create a database for wordpress to operate. For this we will first create a new Databases and a new user with rights to operate the database

Enter mariadb console

sudo mysql

Now create DB and user, replace passowrd with your own password in ""

create database wordpress_db;
create user wordpress@localhost identified by 'password';
grant all privileges on wordpress_db.* to wordpress@localhost identified by 'password';
flush privileges;
exit;

Wordpress Database Connection

We now need to configure wordpress to connect to the newly created database

Make a copy from sample config file

sudo cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php

Edit the config file

sudo nano /var/www/wordpress/wp-config.php

Now change the Database Name, Database User and Database password. We also need to change the unique keys. These keys can be generated from the HERE

After editing the config file, re-apply the permissions

sudo chown www-data:www-data -R /var/www/wordpress

Setup NGINX configuration

We need to setup the webserver configuration. Here we will set up the wordpress at localhost and with HTTP. For production build you would need to configure HTTPS and SSL certificate. Refer this link for production config LINK

warning

Match the php(version)-fpm at two places in the config

Create config file

sudo nano /etc/nginx/sites-available/wordpress

Add this to the config file

upstream php-handler {
server unix:/var/run/php/php8.3-fpm.sock;
}

server {
listen 80;
listen [::]:80;
server_name localhost;

root /var/www/wordpress; # Change to proper path
index index.php;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
#fastcgi_pass php;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # Change the php version
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}

Then link the file and enabled for NGINX to start serving

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress

Test the NGINX configuration

sudo nginx -t

If everything is ok, we will restart NGINX and PHP-FPM for the changes to take effect

sudo systemctl restart nginx
sudo systemctl restart php8.3-fpm
danger

We need to delete the default config for NGINX so that out localhost points to the wordpress instead of default /var/www/html

Remove default in site-available & site-enabled and also remove the /var/www/html folder

Web Installation

After this you should now be able to access wordpress over localhost in the browser. Add the site title, wordpress user, password, your email and proceed with the installation.