How to Install WordPress Using WP CLI
In previous articles, we covered how to set up a full LEMP stack in Ubuntu 22.04, configure Nginx for shared hosting, secure connections with SSL certificates, and ensure availability over HTTPS.
One of the great advantages of building your own LEMP stack is the ability to customize the configuration and add additional modules to develop the ultimate hosting platform for WordPress that outperforms all competition.
In this guide, we’ll delve deeper into optimizing the system by adding modules, adjusting configurations, and applying various forms of caching (page, browser, and object).
Prerequisites
To perform the following steps, ensure you have WP-CLI installed and root access. Switch to the root user if you are not already:
sudo -i
For convenience and to avoid errors, set the domain name of your new WordPress site in a variable. Replace “example.com” with your actual domain name:
export DOMAIN="example.com"
Verify the variable contains the correct information:
echo $DOMAIN
This should output:
example.com
Navigate to the Document Root
Navigate to the document root of your domain. If you followed our previous tutorial, the document root is located at /var/www/virtual/
and the public directory is htdocs
(e.g., /var/www/virtual/example.com/htdocs
). Adjust the location if necessary:
cd /var/www/virtual/$DOMAIN/htdocs
Download and Install WordPress Core
Use WP-CLI to download, extract, and install the core packages of WordPress. The --skip-content
option ensures a bare installation without default themes and plugins. The --allow-root
option permits WP-CLI to run as the root user:
wp core download --skip-content --allow-root
Create a Database
WordPress requires an empty database. Generate the database name, user name, and password using the domain name as the base. Replace periods with underscores using the sed
command:
export DB_NAME=db_$( echo $DOMAIN | sed -e 's/-/_/g; s/\./_/g' ) \
&& export DB_USER=user_$( echo $DOMAIN | sed -e 's/-/_/g; s/\./_/g' ) \
&& export DB_PW=$(date +%s | sha256sum | base64 | head -c 12)
Verify the variables:
echo Database name: $DB_NAME \
&& echo Database user: $DB_USER \
&& echo Database password: $DB_PW
Example output:
Database name: db_example_com
Database user: user_example_com
Database password: jrweT94KjU13j
Create the database:
mariadb -e "CREATE DATABASE $DB_NAME" && \
mariadb -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_USER@localhost \
IDENTIFIED BY '$DB_PW'" && mariadb -e "FLUSH PRIVILEGES"
Configure WordPress
Link the database to your WordPress instance:
wp core config --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PW \
--dbhost=localhost --dbprefix=prfx_ --force --allow-root --extra-php \
<<PHP
define( 'WP_CACHE', true );
PHP
Set up email, username, website title, and tagline:
export WP_ADMIN_EMAIL="you@your_domain.tld"
export WP_ADMIN_USER="john-doe"
export WP_TITLE="New WP site"
export WP_TAGLINE="Our new WP tagline"
Verify the variables:
echo Email: $WP_ADMIN_EMAIL \
&& echo admin username: $WP_ADMIN_USER \
&& echo WP title: $WP_TITLE \
&& echo WP tagline: $WP_TAGLINE
Example output:
Email: you@your_domain.tld
admin username: john-doe
WP title: New WP site
WP tagline: Our new WP tagline
Install WordPress:
export WP_ADMIN_PW=$(date +%s | sha256sum | base64 | head -c 12) \
&& wp core install --url=https://$DOMAIN --title="$WP_TITLE" \
--admin_user=$WP_ADMIN_USER --admin_password=$WP_ADMIN_PW \
--admin_email=$WP_ADMIN_EMAIL --allow-root && \
wp option update blogdescription "$WP_TAGLINE" --allow-root
Send login information to the admin user’s email:
echo "WP Log-in data for $DOMAIN" > /tmp/$DOMAIN-wp-info.txt \
&& echo "WP admin user name: $WP_ADMIN_USER" >> /tmp/$DOMAIN-wp-info.txt \
&& echo "WP admin password: $WP_ADMIN_PW" >> /tmp/$DOMAIN-wp-info.txt \
&& cat /tmp/$DOMAIN-wp-info.txt | mail -s "New WP credentials" $WP_ADMIN_EMAIL
You should now receive an email with similar info:
WP Log-in data for example.com
WP admin user name: john-doe
WP admin password: QR8kVxFlLKI5
After installing WordPress, the next step is to configure it to suit your needs. This involves installing themes, setting up plugins, and configuring various options to optimize performance and security.
Install and Activate Theme
First, install and activate the default WordPress theme “Twenty Twenty-Four”:
wp theme install twentytwentyfour --activate --allow-root
Update WordPress Settings
Set the date format, time format, and timezone to match your locale:
wp option update date_format d/m/Y --allow-root
wp option update time_format H:i --allow-root
wp option update timezone_string "Europe/Brussels" --allow-root
Configure Permalinks
Set the permalink structure to use the post name, which is more SEO-friendly:
wp rewrite structure '/%postname%/' --allow-root
Install and Activate Plugins
Install essential plugins for backup, security, and caching:
wp plugin install wpvivid-backuprestore wps-hide-login nginx-cache redis-cache \
slim-maintenance-mode disable-wp-rest-api --allow-root
wp plugin activate wpvivid-backuprestore slim-maintenance-mode --allow-root
Set Correct Permissions
Ensure the correct file permissions for the WordPress directory:
chown -R www-data:www-data /var/www/virtual/$DOMAIN/htdocs
find /var/www/virtual/$DOMAIN/htdocs -type d -exec chmod 750 {} \;
find /var/www/virtual/$DOMAIN/htdocs -type f -exec chmod 640 {} \;
Configure Nginx
Edit the Nginx configuration file for your domain:
vi /etc/nginx/sites-available/$DOMAIN
Ensure the following line is present in the “location /” directive to handle permalinks correctly:
location / {
try_files $uri $uri/ /index.php?$args;
}
Test the Nginx configuration:
nginx -t
If the syntax is correct, restart Nginx:
systemctl restart nginx
Review Login Information
Check the login information stored in the temporary file:
cat /tmp/$DOMAIN-wp-info.txt
Example output:
WP Log-in data for example.com
WP admin user name: john-doe
WP admin password: QR8kVxFlLKI5
Configure Redis for Object Caching
If Redis is installed on the server as described in the previous article, you can now set up Redis as the object cache backend:
wp config set WP_REDIS_CLIENT phpredis --allow-root
wp config set WP_REDIS_SCHEME unix --allow-root
wp config set WP_REDIS_PATH /var/run/redis/redis.sock --allow-root
If you host multiple sites, ensure each site uses a different Redis database by incrementing the database number.
With the following command we can determine which database numbers have already been used.
grep -r "WP_REDIS_DATABASE" /var/www/virtual/*/htdocs/wp-config.php
The output should look something like this:
/var/www/virtual/example.biz/htdocs/wp-config.php:define( 'WP_REDIS_DATABASE', '2' );
/var/www/virtual/example.org/htdocs/wp-config.php:define( 'WP_REDIS_DATABASE', '1' );
/var/www/virtual/example.net/htdocs/wp-config.php:define( 'WP_REDIS_DATABASE', '0' );
In this example, the numbers 0,1,2 and 3 have already been applied and thus can no longer be used to configure the current domain. In principle, it does not matter which number you use first as long as it has not been used before and between 0 and 15. However, for consistency, we take the number that follows the last number used, so in this case 3.
wp config set WP_REDIS_DATABASE 3 --allow-root
Activate the Redis cache plugin:
wp plugin activate redis-cache --allow-root
wp redis enable --allow-root
Configure Nginx Cache
Assuming Nginx FastCGI Page Cache is already configured as described in this article, we need to enable Nginx caching plugin and set the cache path:
wp option update nginx_auto_purge 1 --allow-root
wp option update nginx_cache_path '/var/run/nginx-cache/' --allow-root
wp plugin activate nginx-cache --allow-root
Clean Up
Remove the default index.html
file:
rm index.html readme.html
Conclusion
Congratulations! You’ve successfully installed and configured WordPress using WP-CLI on your custom LEMP stack. By following this guide, you’ve not only set up a robust and secure WordPress environment but also optimized it for performance and scalability.
Recap
- Installation: We started by downloading and installing the core WordPress files using WP-CLI, ensuring a clean and efficient setup.
- Database Configuration: We created a new database and linked it to our WordPress installation, ensuring secure and organized data management.
- Initial Setup: We configured essential settings such as the site title, admin user, and email, and installed a default theme.
- Optimization: We installed and activated crucial plugins for backup, security, and caching, and set up Redis for object caching to enhance performance.
- Nginx Configuration: We adjusted the Nginx configuration to handle permalinks correctly and set up caching to further improve site speed.
Next Steps
With your WordPress site up and running, you can now focus on creating content and customizing your site to meet your needs. Here are a few suggestions for next steps:
- Customize Your Theme: Explore different themes and customize them to match your brand.
- Install Additional Plugins: Consider adding plugins for SEO, social media integration, and analytics to enhance your site’s functionality.
- Regular Backups: Set up regular backups to ensure your data is safe and can be restored in case of any issues.
- Monitor Performance: Use tools like Google PageSpeed Insights and GTmetrix to monitor and improve your site’s performance.
By taking these steps, you’ll ensure your WordPress site remains fast, secure, and user-friendly.