Setting up a dynamic website with nginx on Ubuntu 20.
Try to make a local link from your html index page to any php page. Doesn't work, right? It is because your server cannot process files with php extension by itself. It needs help from what is called SAPI or Server Application Programming Interface. There are many SAPIs for php but you will use fpm (aka fast process manager). Fpm is not going to be a part of your server and it is not a separate server itself. Think of it as a very simple and effective helper that controls the number of php-processes you run, their upgrade and restart frequencies. It also deals with possible memory losses that php is so infamous for. Another distinctive difference between fpm and a server is that fpm does not use http protocols; rather, it uses its own fastcgi protocol. Anyway, without further adieu let's go ahead and downoad php-fpm and configure nginx so that it can serve you php content!
Installing php-fpm and configuring it.
1. First, let's go to your nginx configuration file and make some changes as illustrated in this link. (you basically just tell nginx to proxy requests to php-fpm via fastcgi protocol).
sudo nano /etc/nginx/sites-available/mydefault
2. Install php-fpm.
apt-get install php7.0-fpm
I would also reccomend to install the following modules, as you will need them later on: php7.0-curl php7.0-cli php-memcache php-memcached php7.0-mysql php7.0-pgsql php7.0-gd php7.0-imagick php7.0-intl php7.0-mcrypt
Check if it is working correctly.
ps aux | grep php-fpm
3. Restart nginx and fpm.
sudo service restart nginx
sudo service restart php7.4-fpm
In case you are unable to restart any of the services, check the logs.
sudo systemctl status nginx or php7.4-fpm
3.This is it, you are good to go. Create a simple php file in your root folder and test if everything is working correctly by typing your domain name in the search engine.
MySQL
1. Follow the guide in the link to install MySQL "server" on your virtual machine: install mysql.
2. You now need to setup a password for the database. You can also change the user name if you wish.
sudo mysql_secure_installation
When you are finished, you can log into your mysql.
sudo mysql
3. Login as a database user and grant all privileges to the new user.
mysql -u root -p
CREATE USER 'lustra'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'lustra'@'localhost' WITH GRANT OPTION;
flush privileges;
4. Now exit from the user mode by typing \q. After that, login to mysql again, this time using your newly created username and password.
mysql -u lustra -p
Once you have set everything up, you can create a new database from scratch using mysql language only through
command line. However, this is time consuming, so let's leave it until later, when we are more advanced and
care about security.
Instead, let us download a GUI called PhpMyAdmin. It provides
an easy way for us to make and manage our databases.
5. Let's start by updating our server's package index.
sudo apt upgrade
We are now ready to install PhpMyAdmin.
sudo apt install phpmyadmin
During the installation proccess, you will be prompted to choose a web server. Since "nginx" is not an option, press Tab and then OK. You will later be prompted to create a database. Since we already have our database created and configured, choose the "No" option.
6. The installation will now finish. For the nginx web server to find and serve the phpMyAdmin files correctly, we’ll need to create a symbolic link from the installation files to nginx’s document root directory:
sudo ln -s /usr/share/phpmyadmin /var/www/lustra/phpmyadmin
We can now access our database through a GUI. Just type https:your_domain_name/phpmyadmin in your web browser.
Now, there is a whole bunch of security stuff you can add to this, and you can go ahead and explore your options following this guide for that. I will just go ahead and create a basic database in phpmyadmin. Watch this video if you are not sure how to do this.