Hi All,
I am working on a personal project that will involve Node.js, React and MongoDB. This project uses Ubuntu, Nginx and MongoDB. The project will eventually be a web hosting control panel that will allow end users to host Node based apps and or PHP web applications, that can talk to either a MongoDB or MySQL based database.
I will be posting more blog articles around the work that I am doing on this project, such articles will include how to install this software, configure this or that, basically anything that I feel is worth sharing.
In this first article however I am going to explain how to Install Nginx, PHP and MySQL onto Ubuntu 20.04.
Installing Nginx
Lets check for any updates to our system, and then use apt when can then install nginx:
1 2 |
sudo apt update sudo apt install nginx |
Using UFW lets first check the status to see if it’s running, if not lets enable it, then allow nginx through the firewall.
1 2 3 |
sudo ufw status sudo ufw enable sudo ufw allow 'Nginx Full' |
You should now be able to open a web browser and type in the hostname or ip address of your linux box and be presented with the Nginx welcome page.
Installing MySQL
Now that we have installed Nginx we can now install MySQL.
1 |
sudo apt install mysql-server |
Then we can run security installation script.
1 |
sudo mysql_secure_installation |
Follow the on screen prompts.
Once done you can test the connection to the mysql server by running.
1 |
sudo mysql |
And to exit from the MySQL console just type exit
Installing PHP
Unlike Apache, Nginx requires an external program to handle PHP processing and act as a bridge between the PHP interpreter itself and the web server. This leads to better overall performance but does not some additional configuration. We need to install php-fpm which stands for “PHP fastCGI process manager” and to tell nginx to pass PHP requests to this software for processing. Next we will need php-mysql a PHP module that allows PHP to communicate with MySQL based databases. Core PHP Packages will automatically be installed as dependencies.
1 |
sudo apt install php-fpm php-mysql |
Configure Nginx to Use PHP
By default Nginx in ubuntu 20.04 has one server block enabled and is configured to serve documents out of a directory at /var/www/html.
For my project I didn’t want this, instead I wanted the following /home/[user]/public_html
for every new user to have the public_html folder created within their home directory, I needed to create this folder within the /etc/skel directory.
I also wanted every user to have their own Nginx conf file for their site. To do this I created the conf file at the following location sudo nano /etc/nginx/sites-available/example_domain where example_domain is the domain of the customer.
The contents of each file is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
server { listen 80; listen 443 ssl; server_name hostedportal.net www.hostedportal.net; ssl_certificate /etc/ssl/certs/localhost.crt; ssl_certificate_key /etc/ssl/private/localhost.key; root /home/joe/public_html; index index.php index.html index.htm; #Root Location location / { root /home/joe/public_html; index index.php index.html index.htm; try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } #Begin Node include /home/joe/public_html/nodeapps/*.conf; #End Node location ~ /\.ht { deny all; } } |
Once I was happy with the config, I needed to activate the config, as follows.
1 |
sudo ln -s /etc/nginx/sites-available/example_domain /etc/nginx/sites-enabled/ |
Again example_domain being the domain in question.
To make sure there are no errors with the configuration:
1 |
sudo nginx -t |
If all ok then I can reload nginx.
1 |
sudo systemctl reload nginx |
Further Config
Next I wanted it so that whilst a customer is waiting on their domain name to resolve to my server, they can access and test their website by going to the following address http://servername/~user
To do this I needed to edit the default sites-available config and /etc/nginx/fastcgi_params
first the default conf file, I replaced with the following content, after a making a backup first!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html index.php; server_name _; # PHP in home directory location ~ ^/~(.+?)(/.*\.php)(.*)$ { alias /home/$1/public_html; try_files $2 =404; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_intercept_errors on; include fastcgi_params; fastcgi_param SCRIPT_NAME /home/$1/public_html$fastcgi_script_name; } # Home directories location ~ ^/~(.+?)(/.*)?$ { alias /home/$1/public_html$2; } } |
Next I then needed to add the following line to the bottom of /etc/nginx/fastcgi_params
1 |
fastcgi_param SCRIPT_FILENAME $request_filename; |
I then saved the file, and reloaded Nginx.