Monday, January 25, 2016

Install & setup NGINX, PHP, MYSQL and FTP in Ubuntu 19.04

This post will describe how to install how to set up a brand new website hosting using the following software technologies: webserver: Nginx, language: PHP, database: MySQL, and set up ssh secure shell and FTP access under Linux Ubuntu 19.04.
Knowing how to install these services is essential when developing web applications, it is also very useful while taking comprehensive course projects, which prefer local installations. 
 

Please take a look at the video:


We start with installing the ssh server so in the console ( terminal ) start typing the following commands:
sudo apt install openssh-server
then we start the server:
sudo systemctl start sshd
from now on we can access our server via: ssh 192.168.100.1 -l myuser just replace your IP address and the user parameters

NGINX
install the Nginx server
sudo apt-get install nginx
edit the sites directive and ...
sudo nano /etc/nginx/sites-available/default
and place the following information, replacing the server_name IP address with your own(check it with the 'ifconfig' command) and if you wish the root directory of the server:
 
 server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

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

    server_name 192.168.100.1;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

write out with ctrl+o and exit.

open the Nginx configuration file
sudo nano /etc/nginx/nginx.conf
and make sure that the user www-data; is present!
restart Nginx to apply the settings
sudo systemctl restart nginx
then if you type: http://your_ip_address/ the webserver should give you a webpage!

MYSQL

install the MySQL server
sudo apt install mysql-server
automatically set up default permissions for databases, remove users and restrict access
sudo mysql_secure_installation

change ownership of MySQL directory in order to have database access from the MySQL client / or API interface running on PHP applications
sudo chown mysql:mysql mysql/ -R
change connection of MySQL from a socket to password authentication:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
next, apply the changes with:
FLUSH PRIVILEGES;
EXIT
optional: add DB permissions in order for the MySQL interface used by PHP to access the databases and tables
open up MySQL. When asked for password type "password"
mysql -u root -p
give privileges: replace your_database with the DB you want to access from within PHP and your_user with a chosen user, you want to give privileges on:
GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'%' WITH GRANT OPTION;

FTP SERVER
install FTP server vsftpd
sudo apt install vsftpd
edit the vsftpd configuration file:
sudo nano /etc/vsftpd.conf
make sure that these are uncommented (without ; in front of the line)
enable local_enable = YES;
write_enable = YES;
local_umask = 022;
and add
local_root=/usr/share/nginx/html
to be able when connecting with FTP client to jump straight to the website directory.

Note:
use only one of these, the other option should be set to NO:
for IPv4: listen=YES
for IPv6: listen_ipv6=YES

restart vsftpd to apply the new changes
sudo systemctl restart vsftpd

PHP
install php language
sudo apt install php-fpm php-mysql
edit php config files:
sudo nano /etc/php7.3/fpm/php-fpm.conf

make sure that
user = www-data
group = www-data
are set in: sudo nano /etc/php7.3/fpm/pool.d/www.conf

in the following file you can set up error logging:
sudo nano /etc/php7.3/fpm/php.ini
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
restart the PHP service to accept the changes:
sudo systemctl restart php-fpm

PHPMYADMIN
install PHPMyAdmin
sudo apt install phpmyadmin
(& create PHPMyAdmin with user and password of your choice)

create a symbolic link from the default HTML directory of Nginx to PHPMyAdmin so it can be browsed via http://your_host/phpmyadmin (where your_host could be 127.0.0.1 or you IP address)
sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/html

change ownership of default Nginx directory to be created and owned by www-data
sudo chown www-data:www-data /usr/share/nginx/html/ -R
give permissions of any owner and member of this www-data group to read and write files in the default website directory (for FTP and console access) 
sudo chmod -R 775 /usr/share/nginx/html/

set sticky bit for anyone who is a member of the group www-data to create new files and directories with default read write and execute permissions.
sudo chmod g+rwxs /usr/share/nginx/html
add our user $USER to be a member of the www-data group, which is the default group of Nginx server (to enable writing into files)
add our user $USER to the www-data group
sudo usermod -a -G www-data $USER
note: you'll have to re-login in the server to see the effect of creating new files and directories or the alternative is to type: newgrp www-data and test the changes!

That's it, enjoy and happy coding. And if you want to learn more on Ubuntu administration you can try this course

Subscribe To My Channel for updates

Burnout or toxic culture ?

Outsourcing companies are hell to be in for an experienced programmer, because managers are being allowed to mistakes, which are covered, th...