Blog

How to Set Up a WordPress Website on LAMP Server via Command Line

Setting up a WordPress website on a LAMP server can seem daunting, especially if you’re new to server management and the command line. However, with the right guidance, the process can be straightforward and efficient. This tutorial is designed for junior developers looking to get hands-on experience with server management and WordPress installation. By the end of this guide, you’ll have a fully functional WordPress site running on a LAMP server, all set up using the command line.

In this lesson, we assume that your server is already equipped with a LAMP stack (Linux, Apache, MySQL, PHP). We’ll walk you through creating a MySQL database, configuring Apache for WordPress, and setting up WordPress itself. This step-by-step guide ensures you understand each part of the process, enhancing your skills and confidence in managing web servers.

So, grab a cup of coffee, open your terminal, and let’s dive into the world of WordPress!

Step 1: Access Your Server

First, connect to your server via SSH. Open your terminal and run:

ssh root@your_server_ip

Replace your_server_ip with the IP address of your DigitalOcean droplet.

Step 2: Update Your Server

Update the package index and upgrade the installed packages to the latest versions:

apt update && apt upgrade -y

Step 3: Create a MySQL Database and User for WordPress

Log in to MySQL as root:

mysql -u root -p

Create a database and user for WordPress:

CREATE DATABASE wordpress;

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

GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';

FLUSH PRIVILEGES;

EXIT;

Replace your_password with a strong password.

Step 4: Download and Configure WordPress

Move to the web directory and download WordPress:

cd /var/www/

mkdir your_directory_name

cd your_directory_name

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

tar -xvzf latest.tar.gz

mv wordpress/* .

rm -rf wordpress latest.tar.gz

Step 5: Configure WordPress

Create a configuration file from the sample file:

cp wp-config-sample.php wp-config.php

Edit the configuration file to add your database details, you can also use FTP to do this:

nano wp-config.php

Update the database details and add Salt keys:

define('DB_NAME', 'wordpress');

define('DB_USER', 'wp_user');

define('DB_PASSWORD', 'your_password');

define('DB_HOST', 'localhost');

Save and exit the file (Ctrl + O, Enter, Ctrl + X).

Add Salt Keys via Nano or FTP.

To generate salt keys run the following command

curl -s https://api.wordpress.org/secret-key/1.1/salt/

Copy and Paste the salt values in place of the dummy values into the wp-config.php file.

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' );

Save and exit the file (Ctrl + O, Enter, Ctrl + X).

Step 6: Set Correct Permissions

Set the appropriate ownership and permissions:

chown -R www-data:www-data /var/www/your_directory_name

chmod -R 755 /var/www/your_directory_name

Step 7: Point Custom Domain Name to Server

Log into your domain hosting provider and edit the DNS records. Add an “A” record @ and point it to the IP address of your website server.

Step 8: Add/Enable .conf file 

Navigate to /etc/apache2/sites-available. You can also use FTP to edit these files.

cd /
cd /etc/apache2/sites-available
touch file_name.conf
nano file_name.conf

Once in the conf file, paste the following, being sure to replace your_directory_name and your_domain with your information.

<VirtualHost *:80>

	ServerAdmin email@youremail.com
  	ServerName your_domain.com
  	ServerAlias *your_domain.com
	DocumentRoot /var/www/your_directory_name

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
	<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

<Directory /var/www/your_directory_name>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

</VirtualHost>

Save and exit the file (Ctrl + O, Enter, Ctrl + X).

Enable the conf file and restart Apache:

a2ensite site_name.conf

systemctl restart apache2

Step 9: Complete the Installation via Web Interface

Open your web browser and navigate to:

http://your_domain_name

Follow the on-screen instructions to complete the WordPress installation.

Best Practices

  1. Regular Updates: Keep your server and WordPress installation updated to protect against vulnerabilities.
  2. Strong Passwords: Use strong, unique passwords for your MySQL database, WordPress admin account, and server root account.
  3. Backup Regularly: Set up regular backups of your WordPress site and database.
  4. Secure MySQL: Beyond mysql_secure_installation, consider configuring a firewall and using SSH keys for additional security.
  5. Optimize Performance: Use caching mechanisms like WP Super Cache or W3 Total Cache and consider using a CDN for better performance.