How to Install WordPress with Apache in Ubuntu 20.04

WordPress is the most popular platform in the world for building websites, be it a blog, an e-commerce website, a business website, a portfolio website, an online business directory, and so on. It is free and open-source, easy to install, learn and use, highly pluggable and customizable too.

Read Also: How to Install WordPress with Nginx in Ubuntu 20.04

This guide shows how to install the latest version of WordPress with Apache in Ubuntu 20.04. It assumes that you have the LAMP stack installed and well configured for hosting websites, otherwise, see our guide:

Installing WordPress in Ubuntu 20.04

1. Once the LAMP stack (Apache, MariaDB, and PHP) installed and configured on the Ubuntu 20.04 server, you can proceed further to download the latest version of WordPress using the following wget command.

$ wget -c http://wordpress.org/latest.tar.gz
Download WordPress in Ubuntu 20.04
Download WordPress in Ubuntu 20.04

2. Once the download is complete, extract the archived file using the tar command as shown.

$ tar -xzvf latest.tar.gz
Extract WordPress Files
Extract WordPress Files

3. Next, move the extracted WordPress directory into your document root i.e. /var/www/html/ and under your website as shown (replace mysite.com with your website’s name or domain name). The following command will create a mysite.com directory and move WordPress files under it.

$ ls -l
$ sudo cp -R wordpress /var/www/html/mysite.com
$ ls -l /var/www/html/
Setup WordPress on Website
Setup WordPress on Website

4. Now set appropriate permissions on the website (/var/www/html/mysite.com) directory. It should be owned by the Apache2 user and group called www-data.

$ sudo chown -R www-data:www-data /var/www/html/mysite.com
$ sudo chmod -R 775 /var/www/html/mysite.com
Set Permissions on WordPress Files
Set Permissions on WordPress Files

Creating a WordPress Database for Website

5. To get started, log into your MariaDB database shell using the following mysql command with -u flag to supply the username which should be root and -p to enter a password that you set for the MySQL root account when you installed the MariaDB software.

$ sudo mysql -u root -p

6. After login, run the following commands to create your site’s database and a database user with privileges as shown. Remember to replace “mysite”, “mysiteadmin” and “SecureP@ssHere!” with your database name, database username, and the user’s password.

MariaDB [(none)]> CREATE DATABASE mysite;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON mysite.* TO 'mysiteadmin'@'localhost' IDENTIFIED BY 'SecureP@ssHere!';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT
Create WordPress Database
Create a WordPress Database

7. Next, move into your website’s document root, create a wp-config.php file from the sample configuration file provided as shown.

$ cd /var/www/html/mysite.com
$ sudo mv wp-config-sample.php wp-config.php

8. Then open the wp-config.php configuration file for editing.

$ sudo vim wp-config.php

and update the database connection parameters (database name, database user, and the user’s password created above) as shown in the following screenshot.

Configure WordPress Database Settings
Configure WordPress Database Settings

Creating Apache VirtualHost for WordPress Website

9. Next, you need to configure Apache webserver to serve your WordPress site using your fully qualified domain name, by creating a Virtual Host for it under the Apache configuration.

To create and activate a new Virtual Host, create a new file under the /etc/apache2/sites-available/ directory. In this example, we will call the file mysite.com.conf (it should end with the .conf extension).

$ sudo vim /etc/apache2/sites-available/mysite.com.conf

Then copy and paste the following configuration in it (replacing the ServerName and ServerAdmin emails with your values).

<VirtualHost *:80>
	ServerName mysite.com
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html/mysite.com
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Create WordPress Apache Virtual Host
Create WordPress Apache Virtual Host

Save the file and close it.

10. Then check the Apache configuration for syntax correctness. If the syntax is OK, enable the new site and reload the apache2 service to apply the new changes.

$ apache2ctl -t
$ sudo a2ensite mysite.com.conf
$ sudo systemctl reload apache2
Enable Website in Apache
Enable Website in Apache

11. Also, disable the default virtual host to allow your new site to load properly from a web browser.

$ sudo a2dissite 000-default.conf
$ sudo systemctl reload apache2
Disable Website in Apache
Disable Website in Apache

Completing the WordPress Installation via Web Interface

12. The final section demonstrates how to complete the WordPress installation using the web installer. So open your browser and navigate using your site’s domain name:

http://mysite.com.

Once the WordPress web installer loads, pick the language you wish to use for installation and click Continue.

Select WordPress Installation Language
Select WordPress Installation Language

13. Next, set your site’s title, administrative username, and password and an email for managing your site content. Then click Install WordPress.

Add WordPress Site Details
Add WordPress Site Details

14. Once the WordPress installation is complete, click on Log in to access your site’s administrative login page.

WordPress Installation Complete
WordPress Installation Complete

15. Now log into your new WordPress website using your administrative credentials (username and password created above) and start to customize your site from Dashboard.

WordPress Login
WordPress Login
WordPress Dashboard
WordPress Dashboard

In this article, we have explained how to install WordPress using Apache as a web server and MySQL as a database system for serving PHP websites.

Next, the crucial step is to secure your WordPress site with SSL. If you have deployed WordPress on a real domain, you can secure the site with Free Let’s Encrypt certificate. If you have deployed WordPress locally on a dummy website for testing or personal use, I suggest you to use a self-signed certificate instead.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

34 Comments

Leave a Reply
  1. My VPS ip auto redirects to my 1st domain. So when I install the 2nd domain and point to VPS ip, it also redirects to the 1st domain @@.

    Reply
  2. Hi admin,

    I try to install another domain to my current VPS.

    I create a virtual host and a separate root folder for my 2nd domain.

    However, it always points to my 1st domain

    Please, help!

    Reply
  3. I found this article very helpful. After doing all I am getting errors when I hit localhost in my web browser.

    It says Database Error.

    is there a way to resolve this?
    is there an article for uninstalling everything as well?
    I would like to install it from scratch again.

    Reply
  4. Hi admin,

    Can I save all my files to /root/MyDomain instead of /var/www/html/MyDomain?

    Because if I save in /var/…. only my homepage work, all other pages show a 404 error.

    (I transfer Mysql and all data from share hosting to VPS, all my files used to be saved in /root/MyDomain. So now I think I should save my file in /root/MyDomain now for old Mysql to work in new VPS)

    Should I do that ??

    Reply
  5. Hi admin, thank you very much for your post.

    However, what does if I need to install 2 or more domains to my VPS ??

    Reply
        • I created a virtual host like this:

          
          	ServerName MyDomain
          	ServerAdmin MyDBUser@localhost
          	DocumentRoot /var/www/html/MYDomain
          	ErrorLog ${APACHE_LOG_DIR}/error.log
          	CustomLog ${APACHE_LOG_DIR}/access.log combined
          

          But my domain not work, only the IP address show WordPress Installing page

          Reply
          • @Vanok,

            It is related to your domain Virtualhost configuration, you need to check and correct if any misconfiguration settings in Apache Vhost…

  6. So I followed this blog/post/tutorial and installed LAMP as well, but when I tried to enter the website domain, I only got a “This site can’t be reached” error, but using the server’s IP works
    How can I make the domain useable?

    Reply
      • I bought a domain on Namecheap, it requires Namecheap nameserver to make changes on DNS.

        Then I changed DNS A record to my IP server.

        But it doesn’t work.

        Reply
  7. Do the instructions change if I only want to install the WordPress site locally on my machine? Particularly, do I need to create an Apache Virtual Host for this? I am simply trying to create a WordPress site using the LAMP stack, that will stay in my machine (for testing and development purposes), and that won’t go live.

    Reply
  8. Potentially dumb conceptual question:

    I’ve read wordpress-centric articles that state that one needs to pay for “hosting” when migrating from wordpress.com to WordPress itself.

    If I have been following your guides, installed the LAMP stack, and then complete this guide, am I essentially hosting myself?

    Are there other articles on this blog about using 3rd parties to host our WordPress content?

    Really appreciate these articles by the way, I am a recent Linux convert, and every day I appreciate that decision more and more.

    Thanks

    Reply
  9. You might want to edit this to be sure the viewers are in the proper location for setting up WordPress?

    # cd /var/www/html
    

    Or did I miss that part?

    Reply
  10. Well done tutorial!

    Installing WordPress in Ubuntu 20.04

    4. Now set appropriate permissions on the website (/var/www/html/mysite.com) directory. It should be owned by the Apache2 user and group called www-data.

    $ sudo chown -R www-data:www-data /var/www/html/mysite.com
    

    Should this be:

    $ sudo chown -$ Apache2:www-data /ar/www/html/mysite.com
    
    Reply
  11. Thanks for the excellent tutorial! I still have the following question:

    Since I only wanted a sample website to experiment with, I used mysite.com as URL (as in the tutorial), without realizing that a site with that URL already exists. When I open in step 12 http://mysite.com in a browser, I don’t see the WordPress installer, but the site of a Webhosting company instead.

    What can I do to see “my” website (on localhost) using this URL?

    Thank you again!

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.