Skip to content

Instantly share code, notes, and snippets.

@debojyoti
Created October 12, 2018 15:58
Show Gist options
  • Save debojyoti/84ffe833641563c351e5f716003970c3 to your computer and use it in GitHub Desktop.
Save debojyoti/84ffe833641563c351e5f716003970c3 to your computer and use it in GitHub Desktop.

NGINX, php, mysql and phpmyadmin setup for linux

Step-1: Install PPA repository for Nginx & PHP

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/nginx

Step-2: Install NGINX

sudo apt-get install nginx

Step-3: Install mysql

sudo apt-get install mysql-server

Step-4: Install php 7.1

sudo apt install php7.1 php7.1-bcmath php7.1-bz2 php7.1-cli php7.1-common php7.1-curl php7.1-fpm php7.1-gd php7.1-intl php7.1-json php7.1-mbstring php7.1-mcrypt php7.1-mysql php7.1-opcache php7.1-pspell php7.1-soap php7.1-tidy php7.1-xml php7.1-xmlrpc php7.1-xsl php7.1-zip

Restart php and nginx

sudo service php7.1-fpm restart
sudo service nginx restart

Step-5: Install phpmyadmin

sudo apt-get update
sudo apt-get install phpmyadmin

When asked to choose between apache and lighttpd choose none and simply press TAB key to go forward.

The next prompt will ask if you would like dbconfig-common to configure a database for phpmyadmin to use. Select "Yes" to continue.

The next prompt shall ask you the password, provide the MySQL root password.

Step-6: Setup one virtual host

i) Keep a backup of the existing www.conf file

sudo mv /etc/php/7.1/fpm/pool.d/www.conf{,.bak}

ii) Create a new file for the new virtual host (site1.localhost.com)

sudo nano /etc/php/7.1/fpm/pool.d/site1.localhost.com.conf

iii) Put the following in the file

[site1.localhost.com]
user = www-data
group = www-data
listen = /run/site1.localhost.com-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = ondemand
pm.process_idle_timeout = 30s
pm.max_requests = 512
pm.max_children = 30

Create proper directory structure and log files

sudo mkdir /var/www/site1.localhost.com/
sudo mkdir /var/www/site1.localhost.com/files
sudo mkdir /var/www/site1.localhost.com/log
sudo touch /var/www/site1.localhost.com/log/access.log
sudo touch /var/www/site1.localhost.com/log/error.log

iv) Put a test file

sudo touch /var/www/site1.localhost.com/files/index.php

v) Create nginx Server Block

sudo nano /etc/nginx/sites-available/site1.localhost.com

Put the following in the file

server {
	listen 80;
	server_name site1.localhost.com;
	root /var/www/site1.localhost.com/files;
	index index.php index.html index.htm;

	access_log /var/www/site1.localhost.com/log/access.log;
	error_log  /var/www/site1.localhost.com/log/error.log notice;
    try_files $uri $uri/ /index.php?q=$uri&$args;
    
	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		include fastcgi_params;
		fastcgi_read_timeout 300;
		fastcgi_intercept_errors on;
		fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
		fastcgi_pass unix:/run/site1.localhost.com-fpm.sock;
	}
}

vi) Link the site

sudo ln -s /etc/nginx/sites-available/site1.localhost.com /etc/nginx/sites-enabled/

vii) Link the phpmyadmin

sudo ln -s /usr/share/phpmyadmin/ /var/www/site1.localhost.com/files

viii) Add the virtual host to hosts file

sudo nano /etc/hosts

Add the new virtual host below localhost with same address

127.0.0.1	localhost
127.0.0.1	site1.localhost.com

ix) Restart nginx and php

sudo service php7.1-fpm restart
sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment