- Sep 10, 2010
- 11,805
- 26,657
I have been thinking of doing a tutorial on this for a long time, but I couldn't get enough time to do so. Now that I have some free time today, I decided to write a tutorial. I hope it helps someone out there.
Prerequisites:
I am assuming that you do not have anything else installed on the VPS, and it is a fresh one. Let's assume the ip of the server to be 10.10.1.100 and the username to be root.
Step 1
Login to the VPS.
I am using mac (the process should be exactly the same for linux), but ssh command should be available for windows as well. You do that by opening up the terminal/command prompt, and then by issuing the following command:
You will be greeted with a password challenge. Entering password can be confusing since the terminal will not show the password as you are typing it. Don't worry about it, type the password and hit enter. You should be logged in.
Variations/Gotchas
1. Some hosts do not allow password based logins by default, like AWS EC2. If you have such a host, and you have a private key file, modify the command for login to the following:
The -i flag here means identity.
2. Some hosts do not provide direct root access, instead they allow you to become a root user after you have logged in. AWS EC2 does this for example. For handling that (feel free to combine with the -i flag in case it requires an identity file as well), login normally, e.g. by issuing:
After logging in, become a root user by issuing:
It might ask for the password again. If it does, enter the password again.
Step 2
Install docker.
This step can be a little daunting, and it is bit of a tutorial by itself. You could follow the official guide and install it by yourself. If you want to do that, read the following guide:
But hey, we are blackhatters right?
I have actually made a convenience script that will do the following:
1) Update the server's operating system.
2) Install git, curl and nginx.
3) Install docker and docker compose.
VT of the convinience script:
Download the script by issuing the following:
It will download dockerize.sh in the same folder you are in right now. Verify that the script downloaded correctly, by issuing:
You will see dockerize.sh in the list of files.
Now, run the script by issuing:
It will install the above mentioned softwares once you run this command.
Variations/Gotchas
1. Your ubuntu may be missing the wget package. If it does, you won't be able to download the script atall, it will error out saying wget is not found. If that happens, issue the following command to install wget, and then continue with the Step 2.
2. During installation, you might encounter the following screens:
screen 1:

If you see this screen, just press enter. and it will continue.
Screen 2:

If you see this screen, press TAB once, and then press enter. It will continue.
Step 3
Install a fresh instance of WP
Trust me, we have already done more than half of the work! Was it very difficult? I guess not, lol. You could use this setup for any kind of apps you can imagine (vpn clients/servers, reverse proxies, forum softwares, python scripts, node.js scripts and many more!). But let's focus on getting our site live first.
Let's first understand what a typical wp installation needs. It has two parts - first is the actual php server, that runs wp itself. Second part is the mysql server, that is the database of the wp installation. Addititionally, we need to access the database time to time, and for that we will install phpmyadmin as well.
Luckily, because you are using docker, you will not have to install any of the dependencies manually. All you need is a docker-compose.yaml file to define what you need to install, and docker will take care of virtualising everything for you. We will also need an .env file for defining the db access details. For keeping everything clean and well maintained, let's keep all our files in ~/sites/project-name (the ~ translates to the home folder, which in our case is /root/, because we are logged in as the root user.). Let's make a directory by issuing:
Then go to the sites folder:
Now, you could create your own docker-compose.yaml file, or for saving time, use this one I have already made for you.
Here's the repository:
VT:
To clone this repository, issue the following (I am considering you already are on ~/sites):
Change the project-name with the name of your project. Make sure there is a space between the repository url and the folder-name. After the command finishes running, go the folder by issuing (change project-name with the folder name you chose):
Now, let's make our .env file by copying the .env.example file provided in the repository. Issue the following command for doing so:
Then open the .env file for editing the db details. We will be using nano editor because it is the easiest to deal with (That's what the vi jokes are all about!). You can navigate to the lines by using your keyboard's arrow keys. You can save the file by pressing ctrl + x then pressing y, then pressing enter.
What you need to change here are these lines:
You could keep the WORDPRESS_DB_USER and WORDPRESS_DB_NAME the same, but be absolutely sure to change the WORDPRESS_DB_PASSWORD and WORDPRESS_ROOT_PASSWORD, because otherwise you are opening doors to the hackers.
Verify that the changes are correct, by issuing:
After you have modified the .env file, issue the following command to install the whole stack, as well as sending the stack to the background (-d achieves that. Without this flag, the stack will keep running in the foreground, and if you exit the terminal, the containers will all stop. You can also press ctrl + z to exit without stopping the containers, just in case you ever need to).
Give it a minute or two to setup completely, and Voila!
You now have phpmyadmin, wordpress and mysql installed. But the question is, how would you now connect these to your domain? How would you setup ssl?
I feel this is where it gets a bit more complicated, and people give up. But believe me, it is not very difficult to do so. Yes, issuing ssl certs can be a little challenging to achieve, but there is a shortcut to that. We will be using cloudflare flexible ssl for avoiding having to setup SSL ourselves.
First, setup Cloudflare with your domain. Follow the official doc if you are not sure how to do that:
Then add 3 A records with the following values (all records should be "proxied", not dns only. Also make sure to change the IP to your server IP):
This will connect yourdomain.com to the server IP. After that, go to the SSL/TLS and check the Flexible SSL option on, like this:

Verify that it works by going to both www and non www version of your domain. It should show you the default nginx page that we installed when we installed the docker convenience script. It should already have the https on, even if our server only listens to port 80.
Then go to the Edge Certificates menu, and make sure 'Always Use HTTPS' is turned on. This will save you from having to setup a rule yourself.
Now, go back to the terminal, and go to /etc/nginx/sites-enabled folder by issuing:
This directory has the nginx configurations for connecting the local containers to the domain (nginx works as a reverse proxy here).
We will create a new config here called yoursitename (change yoursitename with something easy to remember) by issuing:
Inside the editor, copy paste the following code (IMPORTANT: be sure to change yourdomain.com to your domain name):
Press ctrl + x, press y and press enter to save and exit.
Verify there is no mistake in the nginx setting by issuing:
If it says the configuration is correct, then restart nginx service by issuing:
Now try visiting the following URLs:
The first domain should point to the wp installation.
The second domain should redirect to the first domain.
The third domain should point to the phpmyadmin, which you can login with the WORDPRESS_DB_USER and WORDPRESS_DB_PASSWORD (or the root password, with root as username) that you had setup earlier.
Things to note:
Your wp installation is available at:
Your database's files are available at
You can stop the containers by going to ~/sites/project-name and issuing:
Your can prune docker cache (it can become huge if not maintained for a while) by issuing:
You can check if your containers are running fine, by issuing:
You can monitor memory usage by issuing:
You can restart the server anytime you want to by issuing
This will not stop the containers. The containers will all start automatically when the server reboots (so yeah, your wp installation is now fault tolerant as well!).
Did I miss anything? Do you think I could do it in a better way ( I feel like the nginx could also be a part of the stack, but I installed it separately so that people can learn how to make it work without too much sweat)? Please comment below.
Not to mention, if you have any trouble following this guide, please reply here. I will do my best to help you out.
Thank you for taking the time to read this.
Have a nice time with the installation!
Prerequisites:
- A decent spec'd VPS with Ubuntu latest stable version installed, and with root access - Anything with over 2 gigs of RAM should be enough to get started.
- Basic Linux knowledge. Although we will go through each and every step in detail, I still recommend taking the time to learn the basics like how to login to a VPS (extra points if you can login using private keys), view all files, move around inside folders, delete files/folders, edit files, rename and copy files, watch resource usage and so on.
I am assuming that you do not have anything else installed on the VPS, and it is a fresh one. Let's assume the ip of the server to be 10.10.1.100 and the username to be root.
Step 1
Login to the VPS.
I am using mac (the process should be exactly the same for linux), but ssh command should be available for windows as well. You do that by opening up the terminal/command prompt, and then by issuing the following command:
Code:
You will be greeted with a password challenge. Entering password can be confusing since the terminal will not show the password as you are typing it. Don't worry about it, type the password and hit enter. You should be logged in.
Variations/Gotchas
1. Some hosts do not allow password based logins by default, like AWS EC2. If you have such a host, and you have a private key file, modify the command for login to the following:
Code:
ssh -i /path/to/your/key/file [email protected]
2. Some hosts do not provide direct root access, instead they allow you to become a root user after you have logged in. AWS EC2 does this for example. For handling that (feel free to combine with the -i flag in case it requires an identity file as well), login normally, e.g. by issuing:
Code:
Code:
sudo -i
Step 2
Install docker.
This step can be a little daunting, and it is bit of a tutorial by itself. You could follow the official guide and install it by yourself. If you want to do that, read the following guide:
Code:
https://docs.docker.com/engine/install/ubuntu/
But hey, we are blackhatters right?
I have actually made a convenience script that will do the following:
1) Update the server's operating system.
2) Install git, curl and nginx.
3) Install docker and docker compose.
VT of the convinience script:
Code:
https://www.virustotal.com/gui/file/b2ad20975d55a5aeb727c6e6d7864a60bee88ec1a563f2ca8f7936fad2e5185e?nocache=1
Download the script by issuing the following:
Code:
wget https://raw.githubusercontent.com/noc2spam/shelly/main/dockerize.sh
Code:
ls -l
Now, run the script by issuing:
Code:
sh dockerize.sh
Variations/Gotchas
1. Your ubuntu may be missing the wget package. If it does, you won't be able to download the script atall, it will error out saying wget is not found. If that happens, issue the following command to install wget, and then continue with the Step 2.
Code:
apt install wget -y
2. During installation, you might encounter the following screens:
screen 1:

If you see this screen, just press enter. and it will continue.
Screen 2:

If you see this screen, press TAB once, and then press enter. It will continue.
Step 3
Install a fresh instance of WP
Trust me, we have already done more than half of the work! Was it very difficult? I guess not, lol. You could use this setup for any kind of apps you can imagine (vpn clients/servers, reverse proxies, forum softwares, python scripts, node.js scripts and many more!). But let's focus on getting our site live first.
Let's first understand what a typical wp installation needs. It has two parts - first is the actual php server, that runs wp itself. Second part is the mysql server, that is the database of the wp installation. Addititionally, we need to access the database time to time, and for that we will install phpmyadmin as well.
Luckily, because you are using docker, you will not have to install any of the dependencies manually. All you need is a docker-compose.yaml file to define what you need to install, and docker will take care of virtualising everything for you. We will also need an .env file for defining the db access details. For keeping everything clean and well maintained, let's keep all our files in ~/sites/project-name (the ~ translates to the home folder, which in our case is /root/, because we are logged in as the root user.). Let's make a directory by issuing:
Code:
mkdir -p ~/sites
Code:
cd ~/sites
Now, you could create your own docker-compose.yaml file, or for saving time, use this one I have already made for you.
Here's the repository:
Code:
https://github.com/noc2spam/wp-on-docker/
VT:
Code:
https://www.virustotal.com/gui/file/81d1d00bfdc28e33bcdaf4a6dd04844483b3eecfe61d366ec94fc2f96e5f58c4?nocache=1
Code:
git clone https://github.com/noc2spam/wp-on-docker/ project-name
Change the project-name with the name of your project. Make sure there is a space between the repository url and the folder-name. After the command finishes running, go the folder by issuing (change project-name with the folder name you chose):
Code:
cd project-name
Now, let's make our .env file by copying the .env.example file provided in the repository. Issue the following command for doing so:
Code:
cp .env.example .env
Then open the .env file for editing the db details. We will be using nano editor because it is the easiest to deal with (That's what the vi jokes are all about!). You can navigate to the lines by using your keyboard's arrow keys. You can save the file by pressing ctrl + x then pressing y, then pressing enter.
Code:
nano .env
What you need to change here are these lines:
Code:
https://github.com/noc2spam/wp-on-docker/blob/e7383465ccf1a7bde7cec581be82d121662a5649/.env.example#L2-L5
Verify that the changes are correct, by issuing:
Code:
cat .env
After you have modified the .env file, issue the following command to install the whole stack, as well as sending the stack to the background (-d achieves that. Without this flag, the stack will keep running in the foreground, and if you exit the terminal, the containers will all stop. You can also press ctrl + z to exit without stopping the containers, just in case you ever need to).
Code:
docker compose up -d
Give it a minute or two to setup completely, and Voila!
You now have phpmyadmin, wordpress and mysql installed. But the question is, how would you now connect these to your domain? How would you setup ssl?
I feel this is where it gets a bit more complicated, and people give up. But believe me, it is not very difficult to do so. Yes, issuing ssl certs can be a little challenging to achieve, but there is a shortcut to that. We will be using cloudflare flexible ssl for avoiding having to setup SSL ourselves.
First, setup Cloudflare with your domain. Follow the official doc if you are not sure how to do that:
Code:
https://developers.cloudflare.com/fundamentals/get-started/setup/add-site/
Then add 3 A records with the following values (all records should be "proxied", not dns only. Also make sure to change the IP to your server IP):
Code:
Host: @, IP: 10.10.1.100, TTL: leave it default
Host: www, IP: 10.10.1.100, TTL: leave it default
Host: phpmyadmin, IP: 10.10.1.100, TTL: leave it default
This will connect yourdomain.com to the server IP. After that, go to the SSL/TLS and check the Flexible SSL option on, like this:

Verify that it works by going to both www and non www version of your domain. It should show you the default nginx page that we installed when we installed the docker convenience script. It should already have the https on, even if our server only listens to port 80.
Then go to the Edge Certificates menu, and make sure 'Always Use HTTPS' is turned on. This will save you from having to setup a rule yourself.
Now, go back to the terminal, and go to /etc/nginx/sites-enabled folder by issuing:
Code:
cd /etc/nginx/sites-enabled
We will create a new config here called yoursitename (change yoursitename with something easy to remember) by issuing:
Code:
nano yoursitename
Code:
https://gist.github.com/noc2spam/241b1a0e0b9083b8769c37937463775c
Verify there is no mistake in the nginx setting by issuing:
Code:
nginx -t
Code:
service nginx restart
Code:
yourdomain.com
www.yourdomain.com
phpmyadmin.yourdomain.com
The second domain should redirect to the first domain.
The third domain should point to the phpmyadmin, which you can login with the WORDPRESS_DB_USER and WORDPRESS_DB_PASSWORD (or the root password, with root as username) that you had setup earlier.
Things to note:
Your wp installation is available at:
Code:
~/sites/project-name/wordpress
Code:
~/sites/project-name/mysql
You can stop the containers by going to ~/sites/project-name and issuing:
Code:
docker compose down
Your can prune docker cache (it can become huge if not maintained for a while) by issuing:
Code:
docker system prune -a
You can check if your containers are running fine, by issuing:
Code:
docker ps
You can monitor memory usage by issuing:
Code:
free -m
You can restart the server anytime you want to by issuing
Code:
reboot
Did I miss anything? Do you think I could do it in a better way ( I feel like the nginx could also be a part of the stack, but I installed it separately so that people can learn how to make it work without too much sweat)? Please comment below.
Not to mention, if you have any trouble following this guide, please reply here. I will do my best to help you out.
Thank you for taking the time to read this.
Have a nice time with the installation!