Installing WordPress on an Ubuntu server with Nginx provides a robust and efficient platform for your website. This guide will walk you through the process of setting up WordPress using the LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04 LTS.
Installing the LEMP Stack and WordPress
Step 1: Update your package list and install Nginx, MySQL, PHP, and PHP-FPM by running the following commands in the terminal:
sudo apt update
sudo apt install nginx mysql-server mysql-client php php-fpm php-mysql
Note: If you are using Ubuntu version 14.04 or earlier, replace apt
with apt-get
in the commands above.
The php-fpm
package is optional but recommended, as it installs the PHP FastCGI Process Manager to improve backend performance. If you prefer MariaDB over MySQL, you can install it instead, following similar steps.
Step 2: Download the latest WordPress package and extract it into the web server's root directory:
cd /var/www/html
sudo wget https://wordpress.org/latest.zip
sudo unzip latest.zip
cd wordpress
Configure Nginx for WordPress
For this setup, we'll use the domain 127.0.0.1
(localhost). In a production environment, you should use your actual domain name or server IP address.
Step 3: Create a new Nginx server block configuration file:
sudo vim /etc/nginx/sites-available/localhost
Insert the following configuration into the file:
server {
listen 80;
listen [::]:80;
root /var/www/html/wordpress;
index index.php;
server_name 127.0.0.1;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Important: Make sure to modify the following in the configuration file:
server_name
: Replace127.0.0.1
with your domain name or server's IP address.PHP-FPM
version: Update the linefastcgi_pass unix:/run/php/php7.4-fpm.sock;
to match your installed PHP-FPM version. Check your PHP version withphp -v
. If it's version7.3
, the line should befastcgi_pass unix:/run/php/php7.3-fpm.sock;
This configuration tells Nginx to serve the WordPress site from /var/www/html/wordpress
, sets index.php
as the default index file, and configures PHP processing. For more details on Nginx directives, refer to the Nginx Documentation.
After making the necessary changes, save and exit the editor. If you're using vim
, press Escape
, type :wq
, and press Enter
.
Step 4: Enable the new server block by creating a symbolic link to the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/localhost /etc/nginx/sites-enabled
Configure MySQL for WordPress
Step 5: Open the MySQL command prompt with root privileges:
mysql -u root -p
The default root
user in MySQL has administrative privileges. Enter the root password when prompted. If you have another user with sufficient privileges, you can use that instead.
Step 6: Within the MySQL prompt, create a new database for WordPress:
CREATE DATABASE your_database_name;
Note: Replace your_database_name
with a name of your choosing.
Step 7: Create a new MySQL user and grant it full privileges on the WordPress database:
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password';
Note: Replace your_username
and your_password
with a username and a strong password of your choice.
Step 8: Apply the changes and exit MySQL:
FLUSH PRIVILEGES;
EXIT;
Step 9: Navigate to the WordPress installation directory:
cd /var/www/html/wordpress
Create the main configuration file by copying the sample file:
sudo cp wp-config-sample.php wp-config.php
Edit the wp-config.php
file:
sudo vim wp-config.php
Update the database connection details with the information you set earlier:
define( 'DB_NAME', 'your_database_name' );
/** MySQL database username */
define( 'DB_USER', 'your_username' );
/** MySQL database password */
define( 'DB_PASSWORD', 'your_password' );
After updating the file, save and close the editor. If using vim
, press Escape
, type :wq
, and press Enter
.
Final Setup
Step 10: Adjust the directory permissions to ensure WordPress is accessible from the browser:
sudo chmod -R 755 .
The 755
permission grants full access to the owner and read-execute permissions to group and others. For more information on permissions, refer to the chmod
manual by running man chmod
.
Step 11: Restart Nginx to apply the configuration changes:
sudo service nginx restart
Step 12: Open your web browser and navigate to your domain (as specified in the server_name
directive of your Nginx configuration). You should see the WordPress setup page.
Complete the WordPress installation by following the on-screen instructions.
By following these steps, you have successfully installed WordPress with Nginx on your Ubuntu 20.04 LTS server. You can now start building your website using WordPress.
Member discussion