INSTALL LEMP (LINUX, NGINX, MARIADB, PHP) ON UBUNTU 14.04

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04

We created an instance of Ubuntu 14.04 on Amazon Web Services (AWS)

check ubuntu version just for curiosity

$ lsb_release -a
Release: 14.04
Codename: trusty

become root user

$ sudo -s

INSTALL NGINX

update repo

$ apt-get update

install nginx

$ apt-get install nginx

you should see your nginx default page (check aws settings for public ip):
http://52.89.115.179/

nginx commands you might need later

$ service nginx status
$ service nginx stop
$ service nginx restart

INSTALL MARIADB

setup MariaDB repo

$ apt-get install software-properties-common
$ apt-key adv –recv-keys –keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
$ add-apt-repository ‘deb http://ftp.osuosl.org/pub/mariadb/repo/10.1/ubuntu trusty main’

you can check out MariaDB repo from main list, it should be at the end of the file
you’ll see only the compiled version and src as commented out, so we are good to go

$ vi /etc/apt/sources.list

deb http://ftp.osuosl.org/pub/mariadb/repo/10.1/ubuntu trusty main
# deb-src http://ftp.osuosl.org/pub/mariadb/repo/10.1/ubuntu trusty main

we are ready to install mariadb 10.1

$ apt-get update
$ apt-get install mariadb-server

it will ask for password, enter twice on purple screen
never forget this database password

now finish the mariadb installation

$ /usr/bin/mysql_secure_installation

  • enter current password for root: (the one you entered)
  • change root password? (type N)
  • Remove anonymous users? (type Y)
  • Disallow root login remotely? (type N)
  • Remove test database and access to it? (type y)
  • Reload privilage tables now? (type y)

let’s verify mariadb is installed

$ mysql -u root -p
Enter password: (type the root password you given)

now list the databases

$ MariaDB [(none)]> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

$ MariaDB [(none)]> exit;
Bye

you can also look at mariadb status

$ service mysql status

* /usr/bin/mysqladmin  Ver 9.1 Distrib 10.1.7-MariaDB, fordebian-linux-gnu on x86_64
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Server version		10.1.7-MariaDB-1~trusty-log
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/run/mysqld/mysqld.sock
Uptime:			8 min 0 sec

Threads: 1  Questions: 368  Slow queries: 0  Opens: 105  Flush tables: 2  Open tables: 27  Queries per second avg: 0.766

you can restart mariadb like this:

$ service mysql restart

INSTALL PHP

$ apt-get install php5-fpm php5-mysqlnd php5-curl

Note: php5-mysqlnd is very important, it removes the mysqli warnings and also enables the right functionalities for PDO

let’s make php more secure, open php fastCGI process manager config file

$ vi /etc/php5/fpm/php.ini

find cgi.fix_pathinfo and set it to zero (0)

cgi.fix_pathinfo=0

restart fpm to take effect

$ service php5-fpm restart

edit nginx default config file

$ vi /etc/nginx/sites-available/default

follow these steps:

  • add index.php next to index
  • add ip next to server_name
  • comment out static error pages
  • comment out location php block

Match server block with the below:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name your_ip;

       location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;    
        #}

        error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

restart ngins

$ service nginx restart

 

INSTALL PHPMYADMIN

$ apt-get update
$ apt-get install phpmyadmin

  • In purple screen it asks apache or lighttpd, we will select neither of it, press Tab key on your keyboard to skip to the OK button, then press enter
  • Configure database for phpymadmin with dbconfig-common? (press Yes)
  • enter same passwords you entered before (3 times) The first password is actually the phpmyadmin web login password but we make it the same to avoid confusion

now let the nginx server finds the location of phpmyadmin

$ ln -s /usr/share/phpmyadmin /usr/share/nginx/html

enable mcrypt PHP module

$ php5enmod mcrypt

restart php processor

$ service php5-fpm restart

now you should see your phpmadmin page, login with root username and password you have given before

http://52.89.115.179/phpmyadmin

 

TEST PHP

Note: Your document root is

/usr/share/nginx/html/

let’s create an info php file to show the php settings

$ vi /usr/share/nginx/html/info.php

type this save and close the file:
check out php info page

http://52.89.115.179/info.php

<?php
phpinfo();
?>

TEST DATABASE VIA PHP

first disable bind-address and skip-external-locking to allow remote connections to mariadb

$ vi /etc/mysql/my.cnf
#bind-address = 127.0.0.1
#skip-external-locking

then go to phpmyadmin and give remote access privileges to a new user

Enter phpmyadmin  :  http://52.89.115.179/phpmyadmin
Click Users
Click Add user
Username: acmusr (any username)
Host: % (means it can connect from all ips)
Password: (any password)
Retype Password (use same password above)
Check all the grants except Create database with same name
Click Go button at the bottom right

then create a testdb.php file to test the db

$ vi /usr/share/nginx/html/testdb.php

<?php $con = mysql_connect("52.89.115.179”,"acmusr",”acm3480#");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Congrats! Connection established successfully";
}
mysql_close($con); ?>

YEEYYYYY!!

but if you still see

Could not connect: Can’t connect to MySQL server on ‘52.89.115.179’ (111 “Connection refused”)

then probably there is something wrong with your ports.. just google!!!!

Advertisements

One thought on “INSTALL LEMP (LINUX, NGINX, MARIADB, PHP) ON UBUNTU 14.04

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s