WordPress Setup in the Ubuntu server

WordPress Setup in the Ubuntu server

Prerequisites

  1. VM, locally or in the server

Basic Knowledge of concept used in this guide

what is .htaccess

The .htaccess (hypertext access) is a critical WordPress core file used to enable or disable features of websites hosted on Apache. The .htaccess files are also referred to as server configuration files located in your WordPress root directory. By default, WordPress uses the .htaccess files to manage redirects and permalink structures.

Many WordPress plugins also use .htaccess files to operate, including most security plugins and caching plugins. These plugins modify and rewrite the .htaccess files to perform their functions.

read more over to https://www.hostinger.com/tutorials/create-default-wordpress-htaccess-file

we will do the following

  1. install apache2 (for serving WordPress, admin panel, and sites )

  2. install PHP (for WordPress)

  3. additional PHP extensions (required for WordPress )

  4. Mysql server (NOT RECOMMENDED, for connecting WordPress with database )

  5. create a user, db, and grant permission

  6. Test connection with new user

  7. create a directory of the project

  8. create a new WordPress project

  9. configure the newly created site in apache2 with WordPress

  10. Enable Rewrite Module for .htaccess

    sudo apt update
    sudo apt install apache2 -y
    sudo apt install php libapache2-mod-php php-mysql
    sudo apt install mysql-server
    

Please open PORT 80 if you are using Ubuntu in any cloud provider with a security group, inbound traffic rules

Result of the installation of mysql-server

After completing the installation process, we can establish a new database and create a user in MySQL. This user should be granted permission to access the newly created database. We will utilize this user, named DB, to establish a connection from WordPress.

CREATE DATABASE wordpress_DB;
CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'ofijes309i532f!ef';
GRANT ALL PRIVILEGES ON wordpress_DB.* TO 'db_user'@'localhost';

Now let's try to log in with this new user (db_user) from BASH,

we have our db ready, we are now creating a new site and configuring WordPress into it.

sudo mkdir /var/www/wordpress.com

we need to add the directory location of the WordPress site in configuration file which will serve by apache2,

we can do one of two thing

  1. clone the existing repo

  2. create a new one in the directory /var/www/wordpress.com

I am creating a new WordPress project

# Go to your project directory and download the latest WordPress File
cd /var/www/wordpress.com
sudo curl -O https://wordpress.org/latest.tar.gz
#Uncompress the tarball which will generate a folder called “wordpress”.

tar -xvf latest.tar.gz

move everything from WordPress to /var/www/wordpress.com

 sudo mv wordpress/* .
# making sample file config file of our project
sudo mv wp-config-sample.php  wp-config.php
# open the file in editor and make the changes

now provide the Database details to the below highlighted box

So far

  1. install apache2 (for serving WordPress, admin panel, and sites )

  2. install PHP (for WordPress)

  3. additional PHP extensions (required for WordPress )

  4. Mysql server (NOT RECOMMENDED, for connecting WordPress with database )

  5. create a user, db, and grant permission

  6. Test connection with new user

  7. create a directory of the project

  8. create a new WordPress project

  9. configure the newly created site in apache2 with WordPress

sudo vim /etc/apache2/sites-available/wordpress.com.conf
<VirtualHost *:80>
    ServerName 192.168.0.111
    # your document root
    DocumentRoot /var/www/worpress.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

we need to change the permission and ownership to serve the s properly

sudo chown -R www-data:www-data /var/www/wordpress.com
sudo chmod -R 755 /var/www/wordpress.com

Enable our new site , disable default and restart

# Enable pur new site and disable defualt , restrat apache2
sudo a2ensite wordpress.com.conf
sudo systemctl restart apache2

Go ahead in to your browser and access your worpress with your IP address , please make sure port 80 is allowed from security group incase you are in public cloud

Problem: let's say you want to access your post of WordPress like this,

http://IP-OR-DOMAIN/postname/ ,

right now use of the .htaccess file is disabled, WordPress and many WordPress plugins use these files extensively for in-directory tweaks to the web server’s behavior.

for fixing this, we need to do two things

  1. Enable Rewrite Module

  2. Set permalink structure in the project

Edit the Apache configuration file for your website with a text editor.

sudo vim /etc/apache2/sites-available/wordpress.com.conf

add the directory block, your file will look like this

<VirtualHost *:80>
    <Directory /var/www/wordpress.com/>
        AllowOverride All
    </Directory>
    ServerName 192.168.0.111
    DocumentRoot /var/www/wordpress.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enabling the Rewrite Module

Next, we can enable mod_rewrite so that we can utilize the WordPress permalink feature, by doing so we can take advantage of different permalink structure

sudo a2enmod rewrite

This allows you to have more human-readable permalinks to your posts, like the following two examples:

http://wordpress.com/2012/post-name/
http://wordpress.com/2012/12/30/post-name

go ahead in http:/IP-ADDRESS/wp-admin/options-permalink.php

now let's choose the option of POST NAMe and save the changes , this will also create a .htaccess file in root directory of the wordpress project

Notice its just created .htaccess a file in root of the project

Now when you move to your browser and want to access your post like this

IP-OR-DOMAIN/postname , you can easily do so

Congrats, you have setup WordPress site in to ubuntu server,

Additional steps you can do by your own

Do it Yourself

  1. Create A database in Cllud like RDS in AWS or Azure Mysql in Azure,

  2. Import this DB to your new Database,

  3. Implement AWS Secret manager or Azure key vault for getting database credentials in your code instead of hard coded in the config,

Did you find this article valuable?

Support Beyond DevOps by becoming a sponsor. Any amount is appreciated!