How to Install WordPress and Nginx on Debian 12
WordPress is one of the most popular Content Management Systems (CMS) for publishing content on the internet. Here is a step by step guide for setting up WordPress with the LEMP stack (Linux, Nginx, MySQL, PHP) on Debian 12, which is a popular Linux Distro on Virtual Private Servers (VPS).
Overview
- Software Installatiopn
- Start and Enable Nginx Webserver
- Customise the PHP settings
- Start and Enable MariaDB
- Create the MySQL Database for WordPress
- Install and Configure WordPress
- Configure Nginx for WordPress
- WordPress Web Installer
Step1: Software Installation
This guide is written for Debain 12. It will likely work with other Debian versions and Ubuntu too, but you may need to change the php syntax later, by replacing php8.2 with your version. Check your Debian version with
lsb_release -a
Before installing the new software, ensure your Debian system is up-to-date.
sudo apt update && apt upgrade
Step2: Install Software Packages
install the Nging, MySQL (Mariadb) and PHP sofware packages
sudo apt install nginx mariadb-server mariadb-client php-fpm php-mysql php-gd php-xml php-mbstring
Step 2: Start and Enable Nginx Webserver
sudo systemctl enable nginx --now
Verify the status is running ok
systemctl status nginx
Step 3: Customise the PHP settings
edit the php.ini file to tweak some of the setting.
nano /etc/php/8.2/fpm/php.ini
Change the following settings per your requirements. These values will get you started, but might need altering as your site expands.
max_execution_time = 300
memory_limit = 512M
post_max_size = 128M
upload_max_filesize = 128M
restart the module for the settings to take affect
systemctl restart php8.2-fpm
Step 4: Start and Enable MariaDB
systemctl enable mariadb --now
check it is running ok
systemctl status mariadb
and now run the following script to secure the setup
mysql_secure_installation
in general answering Y to most of the questions
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y
Step 5: Create the MySQL Database for WordPress
login to the MariadDB server and specify a password for this new server
mysql -u root -p
now run the commands to create the database and grant privileges to the database user
CREATE DATABASE my_wordpress_db;
GRANT ALL PRIVILEGES ON my_wordpress_db.* TO 'my_wordpress_user'@'localhost' IDENTIFIED BY 'my_wordpress_password';
FLUSH PRIVILEGES;
lets see that new database
SHOW DATABASES;
EXIT
It can get confusing having all these different identities and passwords. So make sure to record them
Item | Value |
mysql root password | somestrongpassswordformysql |
mysql wordpress database | my_wordpress_db |
mysql wordpress user username | my_wordpress_user |
mysql wordpress user password | my_wordpress_password |
URL | futurereboot2.com |
wordpress directory | /var/www/html/futurereboot2/ |
nginx config file for wordpress | /etc/nginx/conf.d/wordpress_futurereboot2.conf |
Step 6: Install and Configure WordPress
ok the foundations are laid, now we can get towards the final stages. Navigate into your html directory and download and unzip wordpress
cd /var/www/html
wget https://wordpress.org/latest.zip
unzip latest.zip
later we might have multiple wordpress installations for other websites, so optionally we can rename the dirctory
mv wordpress futurereboot2
now lets navigate into our wordpress directory and make a copy of the default config file and edit it.
cd /var/www/html/futurereboot2
cp wp-config-sample.php wp-config.php
nano wp-config.php
There are three settings to match our mysql database that we created earlier and likely you need to leave localhost setting
/** The name of the database for WordPress */
define( 'DB_NAME', 'my_wordpress_db' );
/** MySQL database username */
define( 'DB_USER', 'my_wordpress_user' );
/** MySQL database password */
define( 'DB_PASSWORD', 'my_wordpress_password' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
after saving the file, lets correct the owner of the wordpress files recursively.
chown -R www-data:www-data /var/www/html/futurereboot/
Step 7: Configure Nginx for WordPress
there are multiple locations for nginx configurations. Probably the simplest is to make a new config file for each wordpress site inside /etc/nginx/conf.d directory. Lets create a new file here for our wordpress site and edit it
nano /etc/nginx/conf.d/wordpress_futurereboot2.conf
Paste in the content, ensuring to change the domainname to match your domain. Also check your php version (with php -v) and set the /var/run/php path accordingly
server {
listen 80;
server_name futurereboot2.com www.futurereboot2.com;
root /var/www/html/futurereboot2;
index index.php;
access_log /var/log/nginx/futurereboot2.com.access.log;
error_log /var/log/nginx/futurereboot2.com.error.log;
client_max_body_size 100M;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_intercept_errors on;
}
}
after saving the file, restart nginx for the changes to take effect
systemctl restart nginx
Step 8: WordPress Web Installer
Now if all has gone well the installation is complete and we can access the web installer. on your browser, navigate to your domain, replacing http://futurereboot2.com in the example, and you should be redirected to the wordpress installation screen. If you dont yet have a domain and are just testing our on your server, modify the server_name parameter of the nginx config to be the IP address you are using instead, eg.
server_name 192.168.12.5;
and restart nginx.
Troubleshooting and Further Reading
Well done on installing the LEMP stack and WordPress on Debian! For now the site will be HTTP only, to secure with SSL and use HTTPS with LetsEncrypt check out this article. For more detailed information on the parameters refer to the official Nginx WordPress documentation.
Error establishing a database connection
If you see the message Error establishing a database connection when trying to reach your site, it could be an issue with your WordPress username and password. So ensure to set your password correctly in MYSQL with
GRANT ALL PRIVILEGES ON my_wordpress_db.* TO 'my_wordpress_user'@'localhost' IDENTIFIED BY 'my_wordpress_password';
and then retest
The perfect playlist to Immerse yourself in refined electronica while studying or working
Pingback: Unable to create directory uploads – Future Reboot