Introduction
WordPress is the most popular CMS (content management system) on the internet. It allows you to easily set up flexible blogs and websites on top of a MySQL backend with PHP processing. WordPress has seen incredible adoption and is a great choice for getting a website up and running quickly. After setup, almost all administration can be done through the web frontend.
In this guide, we’ll focus on getting a WordPress instance set up on a LEMP stack (Linux, Nginx, MySQL, and PHP) on an Ubuntu 16.04 server.
Prerequisites
- You should have non-root user with sudo privileges. For that, you have to create a user and then grant the sudo privileges to the user.
- You should have installed the LEMP stack on your Ubuntu 16.04 server. Refer this initial server setup guide
When you are finished the setup steps, log into your server as your sudo user and continue below.
Step 1: Create a MySQL Database and User for WordPress
The first step that we will take is a preparatory one. WordPress uses MySQL to manage and store site and user information. We have MySQL installed already, but we need to make a database and a user for WordPress to use.
To get started, log into the MySQL root (administrative) account by issuing this command:
1 |
mysql -u root -p |
You will be prompted for the password you set for the MySQL root account when you installed the software.
First, we can create a separate database that WordPress can control. You can call this whatever you would like, but we will be using wordpress
in this guide to keep it simple. You can create the database for WordPress by typing:
1 |
mysql > CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; |
Next, we are going to create a separate MySQL user account that we will use exclusively to operate on our new database. Creating one-function databases and accounts is a good idea from a management and security standpoint. We will use the name wordpressuser
in this guide. Feel free to change this if you’d like.
We are going to create this account, set a password, and grant access to the database we created. We can do this by typing the following command. Remember to choose a strong password here for your database user:
1 |
mysql > GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'password'; |
You now have a database and user account, each made specifically for WordPress. We need to flush the privileges so that the current instance of MySQL knows about the recent changes we’ve made:
1 |
mysql > FLUSH PRIVILEGES; |
Exit out of MySQL by typing:
1 |
mysql > EXIT; |
Step 2: Configuring Nginx for WordPress
You’ll now need to configure Nginx to deliver traffic to the future WordPress installation. Start by creating a new server block for Nginx.
1 |
sudo nano /etc/nginx/sites-available/your-domain.com.conf |
Then you can paste the following into this new file and save it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
## # Default server configuration # server { listen 80; root /var/www/vhost/your-domain.com/public_html; index index.php index.html index.htm index.nginx-debian.html; server_name your-domain.com; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location / { try_files $uri $uri/ /index.php?$args; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/vhost/your-domain.com/public_html; } location ~ \.php$ { include snippets/fastcgi-php.conf; include fastcgi_params; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_pass php; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } location ~ /\.ht { deny all; } } |
Do not forget to replace ‘your-domain.com’ with your actual domain name.
This is a fairly standard nginx+Wordpress server block to get you up and running. It doesn’t come with caching or gzip bells and whistles, but those are beyond the general scope of this tutorial. The WordPress Codex has some additional options if you’re interested.
Be sure to test the configuration to ensure that it will function correctly.
1 |
sudo nginx -t |
Enable the new WordPress server block by creating a link between the sites-available
and sites-enabled
directories.
1 |
sudo ln -s /etc/nginx/sites-available/your-domain.com.conf /etc/nginx/sites-enabled/ |
If you don’t see any errors, you can reload nginx.
1 |
sudo systemctl reload nginx |
Step 3: Installing the WordPress
Now that our server software is configured, we can download and set up WordPress. For security reasons in particular, it is always recommended to get the latest version of WordPress from their site.
Change into a writable directory and then download the compressed release by typing:
1 2 |
cd /tmp curl -O https://wordpress.org/latest.tar.gz |
Extract the compressed file to create the WordPress directory structure:
1 |
tar xzvf latest.tar.gz |
Now you can copy all the files to the server root.
1 |
sudo mv wordpress/* /var/www/vhost/your-domain.com/public_html |
The last step is configuring permissions for the server root. By giving permissions to the www-data
user, you’ll be able to get automatic updates and more.
1 |
sudo chown -R www-data:www-data /var/www/vhost/your-domain.com/public_html |
Finally, you can navigate to your server’s IP address to begin the famous 5-minute WordPress installation.
1 |
http://YOUR-SERVER-IP/wp-admin |
Conclusion
Nginx configuration differs a lot. You have to be careful while adding and removing the location blocks.
Also, you don’t need to worry about the Nginx WordPress htaccess file. Nginx doesn’t process the htaccess file.
We will How to setup WordPress Nginx Multisite in the upcoming article.
If you still have any doubt in installing WordPress on LEMP stack, let us know that in the command.
Source:
.