Install nginx server and configure a website
In this toturial you will learn how to install and configure nginx server and run a website on virtual host. According to the nginx website, virtual hosts are called Server Blocks on the nginx.
Install nginx on the ubuntu server
sudo apt-get install nginx
Start nginx server
sudo service nginx start
Install php-fpm which is used for the communication with nginx and php.
sudo apt-get install php5-fpm
You should edit and fix some lines in php.ini
sudo nano /etc/php5/fpm/php.ini
The commands are
old: expose_php = On new: expose_php = Off
old: memory_limit = 128M new: memory_limit = 512M
old: error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT new: error_reporting = E_ERROR | E_WARNING | E_PARSE | E_NOTICE
old: display_errors = Off new: display_errors = On
old: post_max_size = 8M new: post_max_size = 64M
old: ;cgi.fix_pathinfo=1 new: cgi.fix_pathinfo=0
old: short_open_tag = Off new: short_open_tag = On
Change the following line, if it is different
sudo nano /etc/php5/fpm/pool.d/www.conf listen = /var/run/php5-fpm.sock
Restart php-fpm
sudo service php5-fpm restart
Create a folder for nginx project
mkdir /var/www/mytesting.com/
chnage the ownership of that project folder
sudo chown -R zalam /var/www/nginx_testing.com/
Set permissions for that folder
sudo chmod 755 /var/www/nginx_testing.com/
Create a file inside that project folder
sudo nano /var/www/nginx_testing.com/index.html
Copy the default ngnix block configuration for this project
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/nginx_testing.com
Edit this configuration file
sudo nano /etc/nginx/sites-available/nginx_testing.com
Replace the code below and nginx_testing.com with your domain.
server { listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 index index.php index.html index.htm; # Make site accessible from http://localhost/ server_name nginx_testing.com; # You can specify another sub domain aswell. # server_name www.nginx_testing.com; root /var/www/nginx_testing; # access_log "/var/logs/nginx_access_log"; # error_log "/var/logs/nginx_error_log"; # enforce www (exclude certain subdomains) # if ($host !~* ^(www|subdomain)) # { # rewrite ^/(.*)$ $scheme://www.$host/$1 permanent; # } # enforce NO www # if ($host ~* ^www\.(.*)) # { # set $host_without_www $1; # rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent; # } # set the expiry of following extensions location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { #expires max; expires 1y; log_not_found off; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; } location ~ ^/(.*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|swf|tar|tgz|txt|wav|xls|xlsx|zip|ttf|eot|svg|woff))$ { try_files $uri @fallback; } # This block will disbale .htacess location ~ /\.ht { deny all; } location / { try_files $uri $uri/ /index.php?u=$request_uri; } location ~ \.php$ { fastcgi_pass unix:/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/nginx_testing$fastcgi_script_name; include fastcgi_params; } # Always place these at the bottom of "server" block. error_page 404 /error_404.html; error_page 500 502 503 504 /error_5xx.html; location = /error_5xx.html { root /var/www/nginx_testing; } }
Now enable the available block by executing this command
sudo ln -s /etc/nginx/sites-available/nginx_testing.com /etc/nginx/sites-enabled/nginx_testing.com
Restart the server to see the changes
sudo service nginx restart
If you get a error “Your PHP installation appears to be missing the MySQL extension which is required by WordPress.”
Remove php-mysql and install it again.
apt-get remove php5-mysql
apt-get install php5-mysql
Your website should working perfectly with nginx server.