How to Install WordPress with Apache on Debian and Ubuntu

0 0

Writing an Apache or WordPress introduction will do no good due to the fact that both of them, combined together, are one of the most used Open Source Web Servers on the Internet today, in fact, Apache runs on 36.9% world web servers and WordPress on one of every 6 websites – Apache with MYSQL and PHP providing the dynamic server gateway interface for WordPress Publishing Content Management.

This topic explains the steps needed to be processed in order to install the latest version of WordPress on top of LAMP, which stands for Linux, Apache, MySQL/MariaDB, PHP and PhpMyAdmin on Debian, Ubuntu and Linux Mint, with basic Apache Virtual Host configurations and MySQL database access through command-line or PhpMyAdmin Web Interface, but be aware that does not cover other important network service configurations, like an IP name mapping provided by DNS server and simply uses the rudimentary system hosts file for IP name transactions (DNS query).

Also, the forward settings are available on almost all Debian systems with slight differences (most of them regarding apache paths), which will be noted at the proper time.

Step 1: Server Basic Configurations

1. First of all, due to fact that there’s no authoritative DNS server on the network, and for this setup Apache Virtual Host is used. We need to map server IP to our virtual (fake) domain name to be able to access it like a real domain name from any browser.

To complete this job open and edit ‘/etc/hosts‘ on local server and your preferred domain name on the “127.0.0.1 localhost” line end. In my case, I’ve taken the domain name as ‘wordpress.lan‘.

$ sudo nano /etc/hosts
Setup Local Domain

After your record has been added you can test it by issuing a ping command on your new domain name.

$ ping wordpress.lan
Ping Local Domain
Ping Local Domain

2. If your server is designed for production and runs only from the command line ( and it should ) and you need to access WordPress domain from a Windows station somewhere on your network then open and modify with a notepad that Windows hosts file located on ‘C:WindowsSystem32driversetc‘ path and on the last line add your Apache Server LAMP IP and your virtual domain name.

Add Hostname Windows Host File
Add Hostname Windows Host File

Again issue a ping command line against your WordPress domain name and the server should response back.

Installing LAMP Stack on Server

3. Now it’s time to install a LAMP stack, run the following ‘apt-get’ command to install Apache, MySQL, and PHP.

$ sudo apt-get install apache2 apache2-utils php libapache2-mod-php php-mysql php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip mariadb-server mariadb-client
Install LAMP Stack on Ubuntu
Install LAMP Stack on Ubuntu

Installing the PhpMyAdmin Administration Tool

4. If you are good with MySQL command line you can skip this step, else install PhpMyAdmin Web Interface – A tool that can help you with administering MySQL databases.

Run the following command line, select Apache web server and do not configure a database for PHPMyAdmin with dbconfig-common.

$ sudo apt-get install phpmyadmin
Install PhpMyAdmin in Ubuntu
Install PhpMyAdmin in Ubuntu
Configure Web Server for PhpMyAdmin
Configure Web Server for PhpMyAdmin
Configure Database for PhpMyAdmin
Configure Database for PhpMyAdmin

5. After PhpMyAdmin had been installed is time to make it accessible for web browsing and for that Apache web server needs to read its configurations file.

To enable PhpMyAdmin you must copy 'apache.conf' PhpMyAdmin configuration to 'conf-available' Apache path and enable the new configuration.

For this, run the following series of commands on Ubuntu and Linux Mint systems.

$ sudo cp /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/
$ sudo mv /etc/apache2/conf-available/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
$ sudo a2enconf phpmyadmin
Enable PhpMyAdmin in Apache
Enable PhpMyAdmin in Apache

On Debian systems, issue the following commands.

$ sudo cp /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/
$ sudo mv /etc/apache2/conf.d/apache.conf /etc/apache2/conf.d/phpmyadmin.conf

6. To access PhpMyAdmin, open a web browser, and navigate to the below address.

http://IP-Address-or-Domain/phpmyadmin/
PhpMyAdmin Login
PhpMyAdmin Login

Creating Apache Virtual Host for Domain

7. The next step is to create a Virtual Host on the Apache web server that will host the WordPress new domain. To create and activate a new Virtual Host, open a text editor and create a new file named, suggestive, 'wordpress.conf' on '/etc/apache2/sites-available/' path by running the following command.

$ sudo nano /etc/apache2/sites-available/wordpress.conf

Add the following directives at the bottom of the file. Save and Close the file.


        ServerName wordpress.lan
        ServerAdmin [email protected]
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

Create Apache Virtual Host for WordPress
Create Apache Virtual Host for WordPress

Then activate the new virtual host with this command.

$ sudo a2ensite wordpress.conf
$ sudo systemctl reload apache2
Active WordPress Virtual Host
Active WordPress Virtual Host

8. To avoid that future Apache error concerning, ServerName FQDN missing open main configuration file '/etc/apache2/apache2.conf', add the following line at the bottom of the file and restart service.

ServerName wordpress.lan
Add ServerName in Apache
Add ServerName in Apache

9. Restart the apache2 service.

$ sudo systemctl restart apache2

Creating WordPress Database for Domain

10. Now is time to create a new database and a new database user for WordPress. There are two ways of doing this, either through the MySQL command line, which is also the most secure way or by using the PhpMyAdmin web tool. On this topic, we cover a command-line way.

But first of all, you need to make your MySQL installation secure by running the following security script and answer YES on all questions to harden your SQL database security settings.

$ sudo mysql_secure_installation
Secure MariaDB Installation
Secure MariaDB Installation

11. Now is time to actually create a WordPress database by connecting to mysql shell as a root user.

$ sudo mysql -u root -p
MariaDB [(none)]> CREATE DATABASE mysite;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON mysite.* TO 'mysiteadmin'@'localhost' IDENTIFIED BY  '[email protected]!';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Installing WordPress on Domain

12. After all Apache nasty server configurations had been made and MySQL database and the administrative user had been created it’s now time to actually perform WordPress installation on our box.

First of all download the latest WordPress archive by issuing the following wget command.

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

13. Next extract WordPress archive and copy all extracted files to Apache Virtual Host DocumentRoot, that will be '/var/www/html' on Ubuntu and Linux Mint systems.

$ sudo tar xvzf latest.tar.gz
$ sudo cp -r wordpress/*  /var/www/html

On Debian systems, run the following commands.

$ sudo tar xvzf latest.tar.gz
$ sudo mkdir -p  /var/www/html
$ sudo cp -r wordpress/*  /var/www/html

14. Before starting WordPress installer make sure that Apache and MySQL services are running and also run the following commands to avoid ‘wp-config.php‘ error file creation – we will revert changes afterward.

$ sudo service apache2 restart
$ sudo service mysql restart
$ sudo chown -R www-data  /var/www/html
$ sudo chmod -R 755  /var/www/html

15. Open a browser and enter your server’s IP or virtual domain name on URL using the HTTP protocol.

http://wordpress.lan/index.php
http://your_server_IP/index.php

16. On the first prompt select your Language and hit Continue.

WordPress Setup Wizard
WordPress Setup Wizard

17. On the next screen enter your MySQL wordpress database name, user, password, and host, then hit on Submit.

WordPress Database Settings
WordPress Database Settings

18. After the installer successfully connects to MySQL database and completes ‘wp-config.php‘ file creation hit ‘Run‘ the install button and provide WordPress installer with a Site Title, administrative username, and password for your blog, email address and finally click on Install WordPress.

WordPress Site Details
WordPress Site Details

19. After the installation completes you can login to your new WordPress website blog using your administrative credentials and starts to customize your blog from Dashboard or add new cool articles for millions of readers worldwide or just you!

New WordPress Dashboard
New WordPress Dashboard

20. One more last step is to revert changes made on '/var/www/html‘ directory and file permissions.

$ sudo chown -R root /var/www/html

That’s all the steps needed for a complete WordPress installation on Debian, Ubuntu, Linux Mint, and most of all Debian based Linux distributions using Apache web server, but still, this subject is so vast that only the basic part has been covered.

For a complete environment, you will also need to install and set up a DNS server, enable complex Apache ‘.htacccess‘ rules and, if security demands it, enforce SSL on a Web server.

Enable HTTPS on WordPress

21. If you want to enforce HTTPS on your WordPress website, you need to install a free SSL certificate from Let’s Encrypt as shown.

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo apt-get update
$ sudo apt-get install certbot python3-certbot-nginx
$ sudo certbot --nginx

22. To verify that your WordPress site uses HTTPS, visit your website at https://yourwebsite.com/ and look for the lock icon in the URL bar. Alternatively, you can check your site’s HTTPS at https://www.ssllabs.com/ssltest/.