
This tutorial builds a WordPress website from scratch on a Linux server that you rent and control yourself. We buy a domain name, point it at a virtual private server, lock the server down, install the LAMP stack (Linux, Apache, MySQL and PHP), add HTTPS encryption, and finish by creating the database and unpacking WordPress ready to be configured.
What we will build
When a visitor types our domain name into a browser, the browser looks the name up through DNS and is sent to the IP address of our server. On the server, the Apache web server receives the request and hands it to PHP, which runs WordPress. WordPress keeps its content in a MySQL database.
What you need
- A domain name from an ICANN-accredited registrar.
- A virtual private server (VPS) running Linux. This post uses Ubuntu 16 or later.
- A Linux terminal with SSH (you can enable Bash on Windows 10).
- Comfort typing commands. Lines that start with
$are typed in the shell, and lines that start withmysql>are typed in the MySQL prompt. Replacefoo.comwith your own domain name throughout.
Step 1: The prep work
There are a few things to do before we touch the server.
Buy a domain name
Search for the domain name you want on a registrar's website to see if it is available. It does not have to be a .com; it can be a .org, .space, .io, or any available top-level domain. Prices typically range from $0.99/year to $1,000/year depending on how popular the name is. I've found that most hover around the $12.99/year mark.
Buy a VPS
There are loads of VPS providers out there, and a simple Google search will find them. If you are just starting out, I recommend a VPS with around 1 GB of RAM and 10-20 GB of SSD storage. The RAM will help with performance, and the storage will be plenty for a basic website. That should cost somewhere between $15-30/year. The VPS needs some form of Linux (this post uses Ubuntu 16+), which is usually offered as a pre-install option when you buy.
Point the domain at the VPS
Take the IP address of the VPS (the provider will give it to you), log on to your registrar, and change your DNS records to point to that IP address. Add a record for foo.com and a record for www.foo.com, both pointing to xxx.xxx.xxx.xxx, which you change to your IP address, with foo.com changed to your domain name. When this is done, anyone who types foo.com or www.foo.com into a browser is directed to your site.
After you save the changes it takes a few minutes (I've found it to be around 20 minutes) for the change to propagate through the DNS databases across the internet. Once that is complete, typing your domain name into your browser takes you to your soon-to-be-developed website.
If you try now, you will either get an unresolved domain name error (because we haven't set up the server yet), or, if your Linux instance has an Apache2 web server installed and running by default, you will see its default page, which looks like the screenshot in Step 2, Part B.
Congratulations, we are now ready to begin our server-side development.
Step 2: Server-side setup
Part A: Set up Linux accounts and security
Now the meat begins. We start by logging in to the new VPS. You should have received default login information (a username, likely root, and a password) from the VPS provider, so you might want to check your email.
Open your favorite Linux terminal and use SSH to log in. Replace foo.com with your domain name. Type yes to the authenticity question, followed by the default password.
$ ssh root@foo.com
We are in, just like in a spy movie. First things first, we are going to secure the server. This consists of the following, but is not limited to: updating and upgrading Ubuntu, changing the default root password to something long and complex (you probably won't ever need it again, but you may want it as a catastrophic backup), adding a user with superuser privileges and setting up SSH keys, disabling root access over SSH entirely, setting up the firewall, and setting up unattended security updates. Let's go through these step by step.
Change the root password. Then enter a SuperSecretPassword.
$ passwd
Update and upgrade.
$ apt-get update
$ apt-get upgrade
Create a new user. Enter a SecondSuperSecretPassword.
$ adduser computer
Give the user superuser (sudo) privileges. Add the line below to the bottom of the file, then press control-x.
$ visudo
computer ALL=(ALL:ALL) ALL
Install the nano text editor (because it's awesome).
$ apt-get install nano
Exit the server.
$ exit
Set up SSH keys for ease of login. Make sure you do this from a secure computer. You will be prompted for your new user password. Be sure to replace foo.com with your domain.
$ ssh-keygen
$ ssh-copy-id computer@foo.com
Log in as the new user. No password is needed now because we have SSH key authentication. If the shell looks strange and you cannot use the up arrows, the new user account may have been created with /bin/sh as its login shell, which symlinks to the dash shell by default. To fix this, type sudo chsh -s /bin/bash, then log out and log back in. Be sure to replace foo.com with your domain.
$ ssh computer@foo.com
Change some permissions for security. The first command (chmod 400) prevents accidental overwriting of our SSH keys. The second command makes our new user the owner of the computer directory, recursively.
$ chmod 400 /home/computer/.ssh/authorized_keys
$ chown computer:computer /home/computer -R
Disable root SSH login and restart the service. You will be prompted for your password.
$ sudo -s
$ nano /etc/ssh/sshd_config
Change the line that says:
PermitRootLogin yes
To the following, then press control-x, followed by y:
PermitRootLogin no
Now restart the SSH service:
$ service ssh restart
Install and set up a firewall with some rules, then enable it.
$ apt-get install ufw
$ ufw allow 22
$ ufw allow 80
$ ufw allow 443
$ ufw enable
Set up unattended automatic security updates.
$ apt-get install unattended-upgrades
$ nano /etc/apt/apt.conf.d/10periodic
Add the following lines to the file (the file may be empty when you first open it), then press control-x, then y.
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";
$ nano /etc/apt/apt.conf.d/50unattended-upgrades
Add the following line to the file if it is not already there. If the section is already there, append the line into the proper section. Then press control-x, then y.
"Ubuntu lucid-security";
We made it. We officially have a secure Linux server that is ready to be worked on.
Part B: Install the LAMP stack (Apache, MySQL and PHP)
Install Apache. The last command should return Syntax OK.
$ sudo apt-get update
$ sudo apt-get install apache2
$ sudo apache2ctl configtest
Open the Apache configuration file. Scroll to the bottom and add the ServerName line below (replace foo.com with your domain name), then press control-x, then y.
$ sudo nano /etc/apache2/apache2.conf
ServerName foo.com
Restart Apache, then allow it through the firewall:
$ sudo systemctl restart apache2
$ sudo ufw allow in "Apache Full"
Go to your domain and see if you see the Apache page below. If you do, everything worked.
Install MySQL. When prompted, put in a good root database password. If you mess this up and need to restart, use the following commands to purge MySQL:
$ sudo apt-get remove --purge mysql-server mysql-client mysql-common -y
$ sudo apt-get autoremove -y
$ sudo apt-get autoclean
$ rm -rf /etc/mysql
$ sudo find / -iname mysql* -exec rm -rf {} \;
The install command is:
$ sudo apt-get install mysql-server
Secure the server with the command below. You will need the root password you just set. Select the security options as you see fit. I recommend: Validate password: n, Remove anonymous users: y, Disallow root login remotely: y, Remove test database: y, Reload privilege tables now: y.
$ mysql_secure_installation
Install PHP.
$ sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
Then open the directory index configuration:
$ sudo nano /etc/apache2/mods-enabled/dir.conf
Change this:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
To this, so that index.php is looked for first. Then press control-x, then y.
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Restart Apache:
$ sudo systemctl restart apache2
Part C: Set up HTTPS encryption
Install certbot:
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-apache
Then run the command below. Enter your email address, agree to the terms, say no to the sharing of your email address (advised), and select redirect all requests to HTTPS.
$ sudo certbot --apache -d foo.com
To test whether this was successful, go to https://www.ssllabs.com/ssltest/analyze.html?d=foo.com&latest.
Part D: Create a MySQL database and user for WordPress, then install WordPress
Type the following command. You will be prompted for the SQL root password again. Choose a good password for the user you are creating, and replace SecretSQLUserPassword with something strong.
$ mysql -u root -p
mysql> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
mysql> GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'SecretSQLUserPassword';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
Install additional PHP extensions:
$ sudo apt-get update
$ sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc
$ sudo systemctl restart apache2
Open the Apache configuration file again and add the block below to the bottom of the file. Then press control-x, then y.
$ sudo nano /etc/apache2/apache2.conf
<Directory /var/www/html/>
AllowOverride All
</Directory>
Now run these commands:
$ sudo a2enmod rewrite
$ sudo apache2ctl configtest
$ sudo systemctl restart apache2
Now run all of the following commands to install WordPress:
$ sudo apt-get install curl
$ cd /tmp
$ curl -O https://wordpress.org/latest.tar.gz
$ tar xzvf latest.tar.gz
$ touch /tmp/wordpress/.htaccess
$ chmod 660 /tmp/wordpress/.htaccess
$ cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
$ mkdir /tmp/wordpress/wp-content/upgrade
$ sudo cp -a /tmp/wordpress/. /var/www/html
$ sudo chown -R computer:www-data /var/www/html
$ sudo find /var/www/html -type d -exec chmod g+s {} \;
$ sudo chmod g+w /var/www/html/wp-content
$ sudo chmod -R g+w /var/www/html/wp-content/themes
$ sudo chmod -R g+w /var/www/html/wp-content/plugins
This command returns a set of values:
$ curl -s https://api.wordpress.org/secret-key/1.1/salt/
Open the WordPress configuration file and use the values returned by the previous command to overwrite the defaults.
$ nano /var/www/html/wp-config.php
The defaults look like this:
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
In addition, replace your database name, username and password in the appropriate lines. See the lines below, but with your own password in there.
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wordpressuser');
/** MySQL database password */
define('DB_PASSWORD', 'password');
And that's it. We have finished with the server side.
A note from 2026
This post was written in 2018, and the steps above are left as I wrote them. Several details have changed since, so check them before copying commands onto a new server:
- Ubuntu 16 has long since reached the end of its support period. Start from a currently supported Ubuntu release.
- The
php-mcryptpackage no longer exists for current PHP releases, andpython-certbot-apachebelonged to the Python 2 era. Certbot's own instructions now describe the current way to install it, and theppa:certbot/certbotrepository is no longer the recommended route. - Current MySQL releases no longer accept
GRANT ... IDENTIFIED BYto create a user. You create the user in one statement and grant privileges in another. - WordPress now defaults to a newer, fuller UTF-8 character set (
utf8mb4) for its database rather thanutf8. - Many VPS providers now offer key-based login from the start, and managed hosting is a common alternative if you would rather not maintain a server yourself.
The overall shape of the stack, a web server handing requests to PHP, with WordPress storing its content in a database, is the same.