How to Create HTTP Apache Server and Install WordPress on VPS with Ubuntu 20.04?

This is a tutorial on how to install WordPress on unmanaged cheap but powerful VPS. Unmanaged VPS has no limits which is one of the problems of the managed VPS from well known hosting companies like SiteGround, WP Engine, and others. Unmanaged VPS has almost dedicated speed performance especially buykvm.net which have KVM dedicated slices.

This tutorial is for Ubuntu 18.04/19.04 or before.

Most used commands(in bold are the commands):

sudo -> run with security privileges (run as admin in Windows)

mv -> move files

* -> all

cd .. -> move down one directory

cd /var/www/domain -> moving trough directories this will open /var/www/domain folder

ll -> show all files in directory

nano -> create edit files (sudo nano .htaccess will create clear .htaccess file)

Login as root with your SSH client. I recommend Putty.

Nano is the best editor for Ubuntu or Debian if for some reason the nano editor is not installed, you can install it with this command:

sudo apt install nano

If you want to go for the cheapest unmanaged VPS plan which costs $2 to $5, depend on the hosting and the plan has only 512 MB RAM, so you need to create a swap file which means the SSD/HDD will be used as RAM. I highly recommend allocating swap with SSD no matter the VPS plan, just for you to be on the safe side.

Before you install or execute something always update and upgrade.

sudo apt update && sudo apt upgrade

To install swap follow this steps:

sudo fallocate -l 1G /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo nano /etc/fstab

Add this line after the comments

/swapfile swap swap defaults 0 0

The easiest and automated way to install Apache, MySql, PHP by installing LAMP STACK:

sudo apt install tasksel
sudo tasksel install lamp-server
sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip

At this point you need to add A record in your domain registrar to point to your VPS IP.

Let’s create folder for the WordPress files.

mkdir /var/www/yourdomainname

This command created folder name yourdomainname under /var/www/

Now we need to start to configure the conf for your domain name. Follow these commands:

cd /etc/apache2/sites-available

ll -> this will list two default configs 000-default.conf  and default-ssl.conf

cp 000-default.conf yourdomainname.conf -> this will copy the 000-default.conf and paste to new file with yourdomainname.conf

Now you need to configure your newly created Apache config for your domain and set up email, domain, and alias.

sudo nano yourdomainname.conf -> this will open the conf file with nano editor

At the top above <VirtualHost *:80> you need to add the directory and your conf file should look like this:

<VirtualHost *:80>

<Directory /var/www/yourdomainname>

Options Indexes FollowSymLinks Muliviews

AllowOverride All

Order allow,deny

allow from all

</directory>

ServerName yourdomainname.com

ServerAlias www.yourdomainname.com

DocumentRoot /var/www/yourudeomainname

ServerAdmin youremailaddress@something.com

</VirtualHost>

Plus there will be some more code about error logs and comments that start with #

ctrl + x -> y -> enter to save changes

Now you need to enable this conf and your Apache server is ready for a WordPress installation.

a2ensite yourdomainname.conf -> with this your conf is enabled

a2dissite 000-default.conf -> with this command you disable the default conf which you don’t really need

systemctl reload apache2 -> reload apache so the changes can take effect

MySql database preparation

mysql

CREATE DATABASE thenameyouwant;

GRANT ALL ON thenameyouwant.* TO ‘nameforuser’ IDENTIFIED BY ‘passwordfortheuser’;

quit

Write down the thenameyouwant, nameforuser, passwordfortheuser you will need them later.

With these commands, we created a database with name thenameyouwant, a user with name nameforuser, and password for the user named passwordfortheuser.

Now we just need to change the upload size (plugins, images).

cd /etc/php/

ll -> this will list all the versions should be 7.2 by default

cd 7.2/apache2/

sudo nano php.ini

ctrl+w (search bar) upload_max change to 2048M (upload_max_filesize 2048M)

ctrl+x -> y -> enter

sudo nano php.ini

ctrl+w post_max and change to 1024M (post_max-size = 1024M)

ctrl+x -> y => enter

Now we can install download and install WordPress

cd /var/www/yourdomainname

wget https://wordpress.org/latest.tar.gz

tar -xzvf latest.tar.gz

These commands above will download and extract all wordpress files under a new folder called WordPress. Now we need to move the files into the yourdomainname folder.

You are already into /var/www/yourdomainname folder but in case you missed something:

cd /var/www/yourdomainname

ll

There will be folder called WordPress -> cd wordpress

sudo mv * .. (move all files from folder wordpress one level under which is the yourdomainname folder where files should be)

sudo mv wp-config-sample.php wp-config.php -> this will rename the wp-config-sample.php to wp-config.php

At this point, if you try to load the domain you need to get Error establishing a database connection.

sudo nano wp-config.php -> lets configure the database, user and password

define( ‘DB_NAME’, ‘thenameyouwant’); -> you choose the database name earlier when you created in mysql

define( ‘DB_USER’, ‘nameforuser’); -> you choose the database user when you created earlier in mysql

define( ‘DB_PASSWORD’, ‘passwordfortheuser’); -> you choose the password for the user created earlier in mysql

ctrl+x -> y -> enter

At this point, you can access the domain and make a new WordPress installation.

Now let’s create .htaccess, enable rewrite mod, enable SSL mod, install SSL, and configure a redirect.

Go to your domain name directory:

cd /var/www/yourdomainname

sudo nano .htaacess 

write/copy/paste the standard WordPress rewrite

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
ctrl+x -> y -> enter

With these steps above I created .htaccess but we need to give apache the right permissions and enable rewrite mod:

sudo a2enmod rewrite

sudo systemctl reload apache2

Now in order to enable .htaccess I need to edit the apache2.conf:

cd /etc/apache2/

sudo nano apache2.conf

go down where the other <Directory> is and add this:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

systemctl reload apache2

At this point I can install let’s encrypt SSL, enable it, and make a server-side redirect, follow these commands:

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update

Now I can install the certbot:

sudo apt-get install certbot python3-certbot-apache
sudo a2enmod ssl
sudo a2enmod headers

With the following command I can get a certificate and automatically enable SSL in one step:

sudo certbot --apache -d yourdomainname.com -d www.yourdomainname.com

Follow onscreen instruction enter your email, accept let’s encrypt terms of service, Y/N for sharing your email, and 2 to redirect to HTTPS. Just two more steps to have up and running SSL.

cd /etc/apache2/sites-available/ -> here you will see the conf you created for your website and the default-ssl.conf if you want to copy the default-ssl.conf and create one with yourdomainname do this:

sudo cp default-ssl.conf yourdomainname-ssl.conf

sudo nano yourdomainname-ssl.conf

sudo systemctl reload apache2

Check this line <VirtualHost _default_:433> the _default_:443 is important to stay that way.

Enter a new line and add these lines:

ServerAdmin youremail@something.com

ServerName yourdomainname.com

DocumentRoot /var/www/yourdomainname

Go down under SSLEngine on and just where the comment SSLCertificateFile directive is needed to write these three lines:

SSLCertificateFile /etc/letsencrypt/live/yourdomainname.com/cert.pem

SSLCertificateKeyFile /etc/letsencrypt/live/yourdomainname.com/privkey.pem

SSLCertificateChainFile /etc/letsencrypt/live/yourdomainname.com/chain.pem

ctrl+x -> y -> enter this is saving the changes

sudo a2ensite yourdomainname-ssl.conf

At this point you should have SSL up and running.

If needed (had problem once with hsts) enter this twice and enable for both non www and www version:

sudo certbot –apache

Lastly, add SSL auto-renew if renew is available (certificate need to be in the last 30 days of expiration) this command will auto-update it for new three months:

sudo crontab -e

Add this command after the comments:

0 0  1 * * /opt/letsencrypt/letsencrypt-auto renew

If for some reason auto-renew fail you can always renew it manually (you will get few emails before the certificate expire)

sudo certbot renew --dry-run

Add permanent redirect to the desired version:

cd sudo nano /etc/apache2/sites-available/yourdomainname.conf

under ServerAlias add this:

Redirect permanent / https://yourdomainname.com -> www to non www redirect

Redirect permanent / https://www.yourdomainname.com -> non www to www redirect

Do not forget to enable expires headers for full caching potential and a lot faster site compared to the shared hostings:

sudo a2enmod expires

If you followed every step you should have up and running WordPress with SSL, .htaccess and everything that needs for caching enabled.

Share:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn

Leave A Comment

Covid-19 coronavirus SiteGround discount STAY HOME STAY SAFE!!!


WP Rocket - WordPress best caching plugin defer CSS & JavaScript fix render blocking WordPress

Related Posts