How to install WordPress on a localhost (Ubuntu/Debian)

1. Install a LAMP stack (apache2, php, mariaDB)

sudo apt update

sudo apt install apache2 mariadb-server

sudo apt install php php-mysql -y

Don’t forget to run

sudo mysql_secure_installation

MySql will ask you to set up a password for the root account.

Check your IP address (ifconfig) and put it in a browser. This will make sure that the Apache server is running.

Now, if you go to /var/www/html, you will find a file named index.html. This is what Apache server shows you when you point your browser to localhost. Now you could delete this file.

The next step is to verify that PHP scripting module is loaded properly in Apache. This allows Apache to run PHP scripts to serve dynamic content. Without it or an alternative scripting language like Python, Apache is only able to serve static pages. To test the module, you may create a simple PHP script to test.

Now, create a file named index.php in /var/www/html folder, and add a simple line

<?php phpinfo(); ?>

This creates a PHP script in the directory /var/www/html/

Point your browser to /localhost. A page will pop up, showing basic info on the PHP version, etc.

By default, files for web server are stored in the /var/www/html folder, which is writable by the root user only.

 2. Install wordpress

Now, go to /var/www/html folder and remove everything. Then, download the latest WordPress using wget, unzip the files, and move all files to the html folder

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

sudo tar xzf latest.tar.gz

sudo mv wordpress/* .

Then, remove the empty wordpress folder and the tar file

sudo rm -rf wordpress latest.tar.gz

3. Change ownership of the WordPress folder and why.

If you plan to update the WordPress installation (themes, plugins, etc.), you will need to allow the www-data user, the account used by the Apache server, to change the files stored in /var/www/html folder.

sudo chown -R www-data: .

4. Create and setup a new wordpress database

To get your WordPress site set up, you need a database. This is where MySQL comes in!

Run the MySQL secure installation command in the terminal window:

sudo mysql_secure_installation

You will be asked a series of questions. There’s no password set up initially, but you should set one in the second step. Make sure you enter a password you will remember, as you’ll need it to connect to WordPress. Press Enter to say Yes to each question that follows.

When it’s complete, you will see the messages “All done!” and “Thanks for using MariaDB!”

Run mysql in the terminal window:

sudo mysql -u root -p

Enter the root password you created. You will be greeted by the message “Welcome to the MariaDB monitor.” Create the database for your WordPress installation at the MariaDB [(none)]> prompt using:

create database wordpressDB;

Note the semicolon at the end of the statement. If the command is successful, you should see this:

Query OK, 1 row affected (0.00 sec)

Grant database privileges to the root user, entering your password at the end of the statement:

GRANT ALL PRIVILEGES ON wordpressDB.* TO ‘root’@’localhost’ IDENTIFIED BY ‘YOURPASSWORD’;

For the changes to take effect, you will need to flush the database privileges:

FLUSH PRIVILEGES;

Exit the MariaDB prompt with Ctrl+D to return to the Bash shell. Or, you can follow the steps of creating a user, a database and then assign the database to the new user, if that is less confusing…

5. WordPress configuration

Open the web browser and open http://localhost. You should see a WordPress page asking you to pick your language. Select your language and click Continue. You will be presented with the WordPress welcome screen. Click the Let’s go! button.

Fill out the basic site information as follows:

Database Name:      wordpressDB
User Name:          root
Password:           <YOUR PASSWORD>
Database Host:      localhost
Table Prefix:       wp_

Click Submit to proceed, then click Run the install.

Fill in the form: Give your site a title, create a username and password, and enter your email address. Hit the Install WordPress button, then log in using the account you just created. Now that you’re logged in and your site is set up, you can see your website by visiting http://localhost/wp-admin.

6. Permalinks configuration

It’s a good idea to change your permalink settings to make your URLs more friendly.

To do this, log into WordPress and go to the dashboard. Go to Settings, then Permalinks. Select the Post name option and click Save Changes. You’ll need to enable Apache’s rewrite module:

sudo a2enmod rewrite

You’ll also need to tell the virtual host serving the site to allow requests to be overwritten. Edit the Apache configuration file for your virtual host:

sudo nano /etc/apache2/sites-available/000-default.conf

Add the following lines after line 1:

<Directory “/var/www/html”>

        AllowOverride All
</Directory>

Ensure it’s within the <VirtualHost *:80> like so:

<VirtualHost *:80>
<Directory “/var/www/html”>
AllowOverride All
</Directory>

Save the file and exit, then restart Apache:

sudo systemctl restart apache2

7. If you want to use the built-in web server that comes with the php CLI tool to assist with web development (to skip configure Apache2; but you still have to set up a development database), you could use the command line below (assuming you are in the root directory of your WordPress).

php -S localhost:8080

Other configures could be found at https://www.php.net/manual/en/features.commandline.webserver.php.

 

8. A few useful MariaDB(or MySQL) commands. Note: There might be a few minor differences among different versions of MySQL or MariaDB. Make sure you check the documentation.

create a user with a password

CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'your_password';

create a database

create database 'database_name';

grant all rights to the database to the user

GRANT ALL PRIVILEGES ON ‘dababase_name’.* TO ‘user_name’@’localhost’

Don’t forget to flush the privileges after this step

FLUSH PRIVILEGES;

show all database users

select user from mysql.user;

show all databases;

show databases;

drop a user or database

DROP User 'user_name'@localhost;
DROP DATABASE 'database_name';

Show all grants of a user

SHOW GRANTS FOR user_name@localhost;