lorem

How to install Wordpress CMS in Ubuntu

Wordpress is a quite popular Content Management System(CMS) in today's world. No wonder you are searching for a blog on how to install and run it on your local machine. So without any further ado let's get straight into it.

 

First of all, there are a couple of prerequisites before installing wordpress. You should install:

  1. Nginx- It is the local server that will host your cms in your local machine
  2. PHP- It is responsible for handling all your backend codes
  3. PHP-FPM - Responsible for bridging php application to web server i.e nginx
  4. MySQL- It is necessary since you need a database to store all your datas

 

After you are done installing all the above softwares, let's move onto downloading Wordpress from https://wordpress.org/download/. After you're done downloading the wordpress.tar.gz file you’ll need to extract it. The command for extracting the .tar file in ubuntu is 

 

tar -xvzf name_of_the_file.tar.gz

 

When you are done extracting the file put it in a specific directory. For me, it was /var/www/wordpress

 

Now we shall configure our nginx. The steps and commands of configuring nginx in ubuntu is given below:

 

  1. Go to the directory /etc/nginx/sites-available
  2. $ sudo cp default wordpress
  3. $ sudo vi wordpress
  4. Delete all the previous configurations and copy paste the configuration given below

 

server {

        listen                  80;

        #listen                  443 ssl;

        #ssl_certificate         /etc/ssl/certs/ssl-cert-snakeoil.pem;

        #ssl_certificate_key     /etc/ssl/private/ssl-cert-snakeoil.key;


 

        server_name demowordpress.local;

 

        root /var/www/wordpress;

    index index.php index.html index.htm;

        access_log /var/www/wordpress/logs/access.log;

        error_log  /var/www/wordpress/logs/error.log info;


 

        #auth_basic "Developer Login";

        #auth_basic_user_file /etc/nginx/.htpasswd;

 

    client_max_body_size 20M;

 

        location = /favicon.ico {

                log_not_found off;

                access_log off;

        }

 

        #location = /robots.txt {

                #allow all;

                #log_not_found off;

                #access_log off;

        #}

 

        # This matters if you use drush

        location = /backup {

                deny all;

        }

 

        # Very rarely should these ever be accessed outside of your lan

        location ~* \.(txt|log)$ {

         #       allow 127.0.0.1;

            #    deny all;

        }

 

        # This location block protects against a known attack.

        location ~ \..*/.*\.php$ {

                return 403;

        }

 

        # This is our primary location block.

        location / {

                index index.php;

                #try_files $uri $uri/ @rewrite;

        #try_files $uri $uri/ /index.php?q=$uri&$args;

        try_files $uri /index.php?$query_string;

                expires max;

        }

 

        # This will rewrite our request from domain.com/node/1/ to domain.com/index.php?q=node/1

        # This could be done in try_files without a rewrite however, the GlobalRedirect

        # module enforces no slash (/) at the end of URL's. This rewrite removes that

        # so no infinite redirect loop is reached.

        location @rewrite {

                rewrite ^/(.*)$ /index.php?q=$1;

        }

 

        # If a PHP file is served, this block will handle the request. This block

        # works on the assumption you are using php-cgi listening on /tmp/phpcgi.socket.

        # Please see the php example (usr/share/doc/nginx/examples/php) for more

        # information about setting up PHP.

        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    location ~ \.php$ {

        fastcgi_pass unix:/run/php/php7.4-fpm.sock;

                fastcgi_index index.php;

                fastcgi_read_timeout 500;

                fastcgi_split_path_info ^(.+\.php)(/.+)$;

                include fastcgi_params;

                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    }

 

        # The ImageCache module builds an image 'on the fly' which means that

        # if it doesn't exist, it needs to be created. Nginx is king of static

        # so there's no point in letting PHP decide if it needs to be servered

        # from an existing file.

        # If the image can't be served directly, it's assumed that it doesn't

        # exist and is passed off to PHP via our previous rewrite to let PHP

        # create and serve the image.

        # Notice that try_files does not have $uri/ in it. This is because an

        # image should never be a directory. So there's no point in wasting a

        # stat to serve it that way.

        #location ~ ^/sites/.*/files/imagecache/ {

    location ~ ^/sites/.*/files/styles/ {

                try_files $uri @rewrite;

        }


 

        location ^~ /files/styles/ {

            try_files $uri @rewrite;

        }

 

        # As mentioned above, Nignx is the king of static. If we're serving a static

        # file that ends with one of the following extensions, it is best to set

        # a very high expiration time. This will generate fewer requests for the

        # file. These requests will be logged if found, but not if they don't

        # exist.

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {

                expires max;

                log_not_found off;

        }

 

        # Configuration for phpMyAdmin

        location /phpmyadmin {

               root /usr/share/;

               index index.php index.html index.htm;

               location ~ ^/phpmyadmin/(.+\.php)$ {

                       try_files $uri =404;

                       root /usr/share/;

                       fastcgi_pass unix:/var/run/php-fpm.sock;

                       fastcgi_index index.php;

                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

                       include /etc/nginx/fastcgi_params;

               }

               location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {

                       root /usr/share/;

               }

        }

        location /phpMyAdmin {

               rewrite ^/* /phpmyadmin last;

        }

 

}

  1. Press “:wq” to save the configuration and come back to the terminal
  2. Type $ ls to check if the configuration is saved
  3. Now, Symbolic link (symlink) the configuration by typing the following commands 

$ sudo ln -s /etc/nginx/sites-avaiable/wordpress /etc/nginx/sites-enabled/wordpress

  1. Go to directory /etc/nginx/sites-enabled/ and check if the symlink is done properly
  2. Now restart nginx by typing $ sudo service nginx restart
  3. To check if nginx is running without any errors put in the following command 

$ Sudo nignx -t

And if it is running successfully it should show a message saying,

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

 

Hurray! We have successfully configured nginx. Now let's configure the host. I always forget this part 😂 .

 

To configure the host we need to go to the directory /etc and type the following command

$ sudo vi hosts

And copy paste this in the file that pops open 127.0.1.1       demowordpress.local

Then type “:wq” to save and quit the file. Thus, creating a host for our local site is complete and we can now move on to making our database.

 

To create a database type in the following commands:

  1. $mysql -uusername -ppassword 

N.B Here, the username and password is of your mysql username and password

  1. $ CREATE DATABASE nameofdatabase;
  2. $ quit

 

Open your browser and go to the configured local site. For me it was demowordpress.local and now configure your wordpress cms.