Skip to main content

Wordpress - LAMP Stack on Ubuntu 22.04

813 words·
Wordpress LAMP Stack Apache MySQL

Wordpress LAMP Stack
#

Install Packages
#

# Update package index
sudo apt update
# Install Apache2, MySQL, PHP
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql -y

# Install additional php extensions for wordpress plugins
sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip -y
  • libapache2-mod-php Allows Apache to interpret and execute PHP files

  • php-mysql Allows PHP to communicate with MySQL databases


MySQL
#

Start & Enable SQL server
#

# Start MySQL server
sudo systemctl start mysql

# Enable service after boot
sudo systemctl enable mysql

Configure SQL server
#

# Run SQL client
sudo mysql

# Set root pw and authentication method to "mysql_native_password"
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'sql-root-pw';

# Exit SQL client
exit
# Run secure installation script
sudo mysql_secure_installation

Create Database & User
#

# Connect to SQL server: Prompt for PW
mysql -u root -p
# Create "wordpress" database
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

# Create "wordpressuser" user and set PW
CREATE USER 'wordpressuser'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql-pw';

# Give wordpressuser full access to database
GRANT ALL ON wordpress.* TO 'wordpressuser'@'%';
# Reload & apply changes
FLUSH PRIVILEGES;

# Exit SQL client
exit

Wordpress Setup
#

Create Website Directory
#

# Create website directory
sudo mkdir /var/www/wordpress.jklug.work

Download Wordpress
#

# Change to tmp directory
cd /tmp

# Download wordpress: Latest version
sudo wget http://wordpress.org/latest.tar.gz

# Extract files
sudo tar -xzvf latest.tar.gz
  • Create additional requirements
# Create .htaccess file
sudo touch /tmp/wordpress/.htaccess

# Create upgrade folder
sudo mkdir /tmp/wordpress/wp-content/upgrade
  • Move files into website directory
# Move files
sudo mv wordpress/* /var/www/wordpress.jklug.work/
  • Set owner and permissions
# Change owner
sudo chown -R www-data:www-data /var/www/wordpress.jklug.work

# Set directory permissions
sudo find /var/www/wordpress.jklug.work/ -type d -exec chmod 750 {} \;

# Set file permissions
sudo find /var/www/wordpress.jklug.work/ -type f -exec chmod 640 {} \;

Secret Key Generator
#

# Run Wordpress secret key generator
curl -s https://api.wordpress.org/secret-key/1.1/salt/

# Shell output:
define('AUTH_KEY',         ',po{/5.(i]|%}6Kxnk7;.c[G#`YVE r_Z,[=-zk0u2wRG5u>zG%EZ/NQ:^ !S0&U');
define('SECURE_AUTH_KEY',  'LmsM3|h~xaptK;rXgEx=K*` KY %wB<]B}Ho%^/*gjGZ2oQUJW-9a`?af3A=(Jiw');
define('LOGGED_IN_KEY',    '`B-%g,%+H#V^l2|;->q`9rXW|hc(c<{+lt_{hOg*B#Ly|E|N+9bl+/q*9fcZ&V }');
define('NONCE_KEY',        'J+:piT-|cRolNHOfe&3|,zC8)7wkYo_h|Fl2>Azx$@v1^R(@itv-LdIQZN6ZDa#N');
define('AUTH_SALT',        'AL}++brpnyAfV,#1JQlbh/ KGNZ);q#hySCB8w*!Cfny,v(x{Sr.x7,?j#T||gK[');
define('SECURE_AUTH_SALT', '|OG35+{=@+8T?d2dpUmYdJc5FKCVuIK3#n(GG[=3Rq@p?ou7G,9-8ET_JM#b@CPz');
define('LOGGED_IN_SALT',   '*A;%l!~JsJNiM XEw@e0A#/1Nx-@2Hc.~MD.f2F3x=@n*&d4+.E:l>lm+|`_Ey/)');
define('NONCE_SALT',       'X,iu=j9NN4C+B>K4 c`x/E*@+Ghln:BZ#E (K:vP7i8I=<?1a ymwtz$Ugs2h/Gn');

Wordpress Configuration
#

# Copy example configuration: Make sure the file ownership stays www-data
sudo cp -p /var/www/wordpress.jklug.work/wp-config-sample.php /var/www/wordpress.jklug.work/wp-config.php
# Edit configuration
sudo vi /var/www/wordpress.jklug.work/wp-config.php
  • Define datbase settings: Database, user, pw & filesystem access
# Edit database settings
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** Database username */
define( 'DB_USER', 'wordpressuser' );

/** Database password */
define( 'DB_PASSWORD', 'mysql-pw' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

# Access filesystem directly: When installing or updating plugins & themes
define('FS_METHOD', 'direct');
  • Add Secret Keys (Replace section)
/**#@+
 * Authentication unique keys and salts.
 *
 * Change these to different unique phrases! You can generate these using
 * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
 *
 * You can change these at any point in time to invalidate all existing cookies.
 * This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         ',po{/5.(i]|%}6Kxnk7;.c[G#`YVE r_Z,[=-zk0u2wRG5u>zG%EZ/NQ:^ !S0&U');
define('SECURE_AUTH_KEY',  'LmsM3|h~xaptK;rXgEx=K*` KY %wB<]B}Ho%^/*gjGZ2oQUJW-9a`?af3A=(Jiw');
define('LOGGED_IN_KEY',    '`B-%g,%+H#V^l2|;->q`9rXW|hc(c<{+lt_{hOg*B#Ly|E|N+9bl+/q*9fcZ&V }');
define('NONCE_KEY',        'J+:piT-|cRolNHOfe&3|,zC8)7wkYo_h|Fl2>Azx$@v1^R(@itv-LdIQZN6ZDa#N');
define('AUTH_SALT',        'AL}++brpnyAfV,#1JQlbh/ KGNZ);q#hySCB8w*!Cfny,v(x{Sr.x7,?j#T||gK[');
define('SECURE_AUTH_SALT', '|OG35+{=@+8T?d2dpUmYdJc5FKCVuIK3#n(GG[=3Rq@p?ou7G,9-8ET_JM#b@CPz');
define('LOGGED_IN_SALT',   '*A;%l!~JsJNiM XEw@e0A#/1Nx-@2Hc.~MD.f2F3x=@n*&d4+.E:l>lm+|`_Ey/)');
define('NONCE_SALT',       'X,iu=j9NN4C+B>K4 c`x/E*@+Ghln:BZ#E (K:vP7i8I=<?1a ymwtz$Ugs2h/Gn');

Apache
#

Domain Name
#

Define the domain name for the server in the main configuration file.

# Open main configuration
sudo vi /etc/apache2/apache2.conf

# Add domain name
ServerName jklug.work

Enable SSL
#

# Enable SSL module
sudo a2enmod ssl

Enable Rewrite Module
#

  • Allows to have more human-readable permalinks to posts
# Enable Apache Rewrite module
sudo a2enmod rewrite

Adjust Apache config: PHP
#

Adjust Apache2 configuration to prefer PHP Files.

# Open dir module
sudo vi /etc/apache2/mods-enabled/dir.conf
  • Set index.php on the beginning of the list, it should look like this:
# /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Virtual Host Configuration
#

# Create virtual host configuration
sudo vi /etc/apache2/sites-available/wordpress.jklug.work.conf
# wordpress.jklug.work.conf
<VirtualHost *:80>
    ServerName wordpress.jklug.work
    ServerAlias www.wordpress.jklug.work
    Redirect permanent / https://wordpress.jklug.work/ # Redirect HTTP to HTTPS
</VirtualHost>

<VirtualHost *:443>
    ServerName wordpress.jklug.work
    ServerAlias www.wordpress.jklug.work
    DocumentRoot /var/www/wordpress.jklug.work

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/wordpress.jklug.work/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/wordpress.jklug.work/privkey.pem

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable Virtual Host Configuration
#

# Enable virtual host configuration
sudo a2ensite wordpress.jklug.work

# Disable default host configuration
sudo a2dissite 000-default
# Test configuration
sudo apache2ctl configtest

# Restart Apache
sudo systemctl restart apache2

Wordpress Website & Login
#

  • Finish initial setup
# Worpress Initial Setup
wordpress.jklug.work
  • After initial setup
# Wordpress user login
wordpress.jklug.work/wp-login.php

# Wordpress Website
wordpress.jklug.work

Wordpress Settings
#

Go to the settings section of the wordpress instance and make sure that WordPress Address (URL) and Site Address (URL) point to https://wordpress.jklug.work.