How to Install WordPress on Nginx-based server

edited September 2014 in Proposed How-Tos

Preparations

First of all, before you will install Wordpress on your server you have to be sure that you already have LEMP stack installed.

LEMP installation guide.

Step One: configuring Nginx to serve Wordpress

Let's start our Wordpress instalation wins creating nginx server block for our site.
sudo nano /etc/nginx/sites-available/wordpress
Paste the following code there:
server {
listen 80;

root /var/www/wordpress;
index index.php index.html index.htm;

server_name domain.com;

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}


location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

location = /favicon.ico {
access_log off;
log_not_found off;
expires max;
}
location = /robots.txt {
access_log off;
log_not_found off;
}

# Cache Static Files For As Long As Possible
location ~*
\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$
{
access_log off;
log_not_found off;
expires max;
}
# Security Settings For Better Privacy Deny Hidden Files
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Return 403 Forbidden For readme.(txt|html) or license.(txt|html)
if ($request_uri ~* "^.+(readme|license)\.(txt|html)$") {
return 403;
}
# Disallow PHP In Upload Folder
location /wp-content/uploads/ {
location ~ \.php$ {
deny all;
}
}
}


This is well tuned Wordpress configuration file with permalinks support.
Save (Ctrl+O) and close the file (Ctrl+X).
Let’s enable the server block by symlinking:
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress
Next, we’ll delete Nginx default server block.
sudo rm /etc/nginx/sites-enabled/default
Now, we’ll tune the main Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Make sure that the number of worker processes is equal to the number of cores in your instance.
user www-data;
worker_processes 1;
pid /run/nginx.pid;
Add use epoll; to the events block.
events {
worker_connections 4096;
multi_accept on;
use epoll;
}

add client_max_body_size and server_tokens off derective. Set keepalive_timeout to 30 seconds.

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 100m;



# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;
Make sure that the whole Gzip settings block looks like this:

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
Save (Ctrl+O) and close the file (Ctrl+X).
And then restart the server:
sudo service nginx restart

Step two: Configure PHP

If you want to upload files more than 2mb to your WordPress site, you have to increase PHP upload size variables in php.ini.
sudo nano /etc/php5/fpm/php.ini
Now, press Ctrl+W and search for “upload_max_filesize“and set it to 100m.
upload_max_filesize=100M
Do the same with post_max_size.
post_max_size=100M
post_max_size needs to be the same size or larger than upload_max_filesize.
Restart PHP.
sudo service php5-fpm restart

Step three: Setting up the MySQL database

In this step, we’ll create the database user and tables.
Go ahead and log into the MySQL shell:
mysql -u root -p
Log in using your MySQL root password, and then we’ll need to create a WordPress database, a user in that database and give that user a new password.
First, let’s make the database (feel free to give it whatever name you like):
CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)
After that, we need to create a new user. Please replace the database, name and password with whatever you prefer:
CREATE USER wordpressuser@localhost;
Query OK, 0 rows affected (0.00 sec)
Set a password for your new user:
SET PASSWORD FOR wordpressuser@localhost= PASSWORD("password");
Query OK, 0 rows affected (0.00 sec)
Finish up by granting all privileges to the new user. Without this command, the WordPress installer will not be able to start up:
GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
Then refresh MySQL:
FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
Exit the MySQL shell:
exit

Installing the WordPress files

We’re almost done.
Let’s proceed to installing WordPress.
First navigate to the site root directory.
mkdir /var/www/
cd /var/www/
Now, download the latest version of WordPress:
wget http://wordpress.org/latest.tar.gz
Extract it from the archive:
tar -xzvf latest.tar.gz
Let’s finish WordPress installation by giving the permissions of /var/www/wordpress to www-data user. It will allow future automatic updating of WordPress plugins and file editing with SFTP.
sudo chown -R www-data:www-data wordpress/
sudo usermod -a -G www-data www-data
You’re done! Your new WordPress site is now ready. Just navigate to your website and finish the installation.
image
 
Sign In or Register to comment.