This guide assumes that you have already installed Ubuntu Server on either a computer or a VM and that you have a fundamental idea of what Linux is.
I found a lot of guides online for doing WordPress with nginx were convoluted and didn't work, so I decided to make this guide for those who want to use WP with nginx. The alternative is using the Apache version, which is the most common and is slightly easier to do method.
Installing WordPress can be easy. I've actually made a bash script that will do everything for you, you just need to make sure to set a few things like your MySQL database root password and your MySQL user password.
If you have issues at any point in this guide, feel free to send me a message on the main site: ttckzoo.com
Additionally, you can use ChatGPT to help, as it's very good at Linux stuff! Just copy and paste whatever error you're getting or describe the issue that you're having. More than likely, ChatGPT will either have a direct answer, or will be able to point you in the right direction.
If you are installing on a rented virtual server, your server name in the website build setup is going to be whatever the server's public IP address is (whatever you use to SSH to it).
If you are testing at home, the server name is going to be whatever the computer's local IP address is (usually starts with 192.168. *. *).
ip a
This command should show you a list of your network adapters. Just look at the numbers at the very left, you'll see 1, 2, 3, etc. These are each of the adapters. Look for one that says something like eth0 or ens30 (the numbers may vary). Make sure you see that it says state UP somewhere in the line for the adapter. If it says state DOWN, it's likely not the right one.
Look at the inet line and you should see a local IP that starts with 192.168. *. * or 172. *. *. *
Hosting a site from your house can be a lot of work, on top of being a liability for your personal network. Furthermore, ISPs technically don't want residential customers to use their service for hosting servers, so they might send you warnings.
If you decide to host the website from your home, you will need to set up port forwarding on your router for ports 80 and 443. The port forward needs to point to the local IP address of your webserver. You will probably need to set up Dynamic DNS so that your domain stays linked to your IP even if it changes.
Look into the best security practices for Ubuntu webservers if you're hosting from home. It's advised you put the webserver on its own properly isolated VLAN, only allow SSH from trusted IP addresses, use passwordless SSH with key pairs if you want, and only allow ports 80/443/SSH port on the server (everything except the VLAN stuff applies to rented servers too). Ideally, no one should be able to SSH to your server unless they're on a specified IP or network. Additionally, you can put your SSH port as some random number between 10000 and 63000 to further obscure the login vector.
nano buildwpsite.sh
#!/bin/bash
# Step 2: Install Nginx
echo "Installing Nginx..."
sudo apt install nginx -y
echo "Nginx installed successfully."
echo ""
# Step 3: Install MySQL
echo "Installing MySQL..."
sudo apt install mysql-server -y
echo "MySQL installed successfully."
echo ""
# Secure MySQL installation
echo "Running MySQL secure installation..."
sudo mysql_secure_installation
# Step 4: Create a MySQL Database and User for WordPress
echo "Creating MySQL database and user for WordPress..."
echo ""
read -sp "Enter MySQL root password: " rootpass
echo
read -sp "Enter password for new MySQL user 'wpuser': " wppass
echo
sudo mysql -u root -p"$rootpass" -e "CREATE DATABASE wordpress;"
sudo mysql -u root -p"$rootpass" -e "CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '$wppass';"
sudo mysql -u root -p"$rootpass" -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';"
sudo mysql -u root -p"$rootpass" -e "FLUSH PRIVILEGES;"
echo "MySQL database and user created successfully."
echo ""
# Step 5: Install PHP and Necessary Extensions
echo "Installing PHP and necessary extensions..."
sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-xml php-mbstring -y
echo "PHP and extensions installed successfully."
echo ""
# Step 6: Download and Configure WordPress
echo "Downloading and configuring WordPress..."
cd /var/www/
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress /var/www/html
echo "WordPress downloaded and configured successfully."
echo ""
# Step 7: Set Permissions
echo "Setting permissions for WordPress directory..."
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
echo "Permissions set successfully."
echo ""
# Step 8: Configure Nginx for WordPress
echo "Configuring Nginx for WordPress..."
read -p "Enter your server name (domain or IP): " servername
sudo bash -c "cat > /etc/nginx/sites-available/wordpress <<EOF
server {
listen 80;
server_name $servername;
root /var/www/html/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
EOF"
echo "Nginx configuration created successfully."
echo ""
# Step 9: Enable the Configuration
echo "Enabling Nginx configuration for WordPress..."
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
echo "Nginx configuration enabled and server reloaded successfully."
Save the file (Ctrl + S), and then exit (Ctrl + X).
Add execution permissions to the file by doing:
sudo chmod +x buildwpsite.sh
sudo ./buildwpsite.sh
Make sure you choose a strong password and write it down somewhere. The root user has unfettered access to the database. You shouldn't really have to access the database, but it's good to remember the password just in case you have to. Also, leaving it with a weak password is not a good idea because hackers could abuse vulnerabilities that let them guess at your database password. The database contains sensitive data, especially if you have customers/users.
Make this password different from the root. This is a database user with less permissions, but the permissions they do have are all related to your site, so it needs to be as secure as the root.
This is your computer's IP address (read "A Few Notes" above...). You don't have to worry much about putting your domain in. Your domain-to-server redirection is all taken care of by Cloudflare (unless you're a savage and want to use LetsEncrypt).
Once the script is done, you'll be able to open your browser, type in the IP address you used earlier, and the WordPress setup page should come up.
Congratulations! You've installed WordPress!
WordPress is its own beast, so you will have to play around with it a bit and maybe watch some guides. With enough practice, you'll see that you can build a nice looking website that would otherwise be nearly impossible to do if you had to code it yourself.