Introduction
In this guide, I’ll walk you through the steps to set up a complete web server environment using Nginx, PHP, and MySQL on Ubuntu Server. This setup is ideal for hosting dynamic websites and applications. We assume that Ubuntu Server is already installed and accessible via SSH.
Table of Contents
IntroductionTable of ContentsPrerequisitesStep 1: Install the Nginx Web ServerStep 2: Install MySQL to Mange Site DataStep 3: Installing PHP and Configure Nginx to use the PHP ProcessorStep 4: Test the Web Server SetupConclusion
Prerequisites
- An Ubuntu 22.04 Server with SSH access
- Basic knowledge of terminal commands
- Permissions to install and configure services
If you can run this command, then you’re good to go.
sudo apt update && sudo apt upgrade
Step 1: Install the Nginx Web Server
Nginx is a popular, high-performance web server used to serve web applications.
- Install Nginx From apt
Nginx is available in Ubuntu’s default repositories, so it is possible to install it from these repositories using the
apt
packaging systemMake sure to update the local package index so we have access to the most recent package listings. Afterward, you can install
nginx
.sudo apt update sudo apt install nginx -y
Verify the nginx already installed by running this command:
sudo systemctl status nginx
It will automatically running as system service.
- Allow Access To Nginx on Firewall
- Nginx Full: This profile opens both port
80
and port443
- Nginx HTTP: This profile opens only port
80
- Nginx HTTPS: This profile opens only port
443
Before testing Nginx web server, the firewall on the system needs to be adjusted to allow access to the service.
List the application configurations that
ufw
:sudo ufw app list
Your output should be a list of the application profiles:
Output Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
The list display three profiles available for Nginx:
It is recommended that you enable only port you’re using.
sudo ufw allow 'Nginx Full'
Verify the change:
sudo ufw status
The results will be look like this:
- Start and Enable Nginx
The Nginx is actually already run as service after it is installed, but if the Nginx not running yet you can start and enable it using commands below:
sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx
Now, you can test accessing the default Nginx landing page by using curl to localhost:
curl -I http://localhost
The output will look like this:
If we received HTTP/1.1 200 OK, then we have our Nginx web server installed. Now we’re ready to serve our content via web server.
Step 2: Install MySQL to Mange Site Data
After we have the web server installed, we now continue to install MySQL, a database management system to store and manage the data for our site.
- Install MySQL using APT
sudo apt install mysql-server=8.0
The MySQL database software is now installed, but it needs to be configured.
- Secure Installation
To secure the installation, MySQL comes with a script that will ask whether you want to modify some insecure defaults. Initiate the script by typing the following:
sudo mysql_secure_installation
The script will ask security options like
VALIDATE PASSWORD PLUGIN
to test passwords and check the strength of password, allowing the users to set only those password which are secure enough. Answer
Y
for yes, or anything else to continue without enabling. For the rest of the questions, you should press
Y
and hit the ENTER
key at each prompt. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.Now for authentication of
root
user, by default MySQL use auth_socket
plugin rather than a password. Let’s switch
auth_socket
to mysql_native_password
so we can use external program such as phpMyAdmin.- Switch Root Authentication to Password
sudo mysql
Next, check which authentication method each of your MySQL user accounts uses with the following command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
This example demonstrates that the root user does in fact authenticate using the
auth_socket
plugin. To configure the root account to authenticate with a password, run the following ALTER USER
command. Be sure to change password
to a strong password of your choosing.ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Then run
FLUSH PRIVILEGES
to tell the server to reload the grant tables and put your new changes into effect:FLUSH PRIVILEGES;
Check the authentication methods again to confirm tat root no longer authenticates using the
auth_socket
plugin:SELECT user,authentication_string,plugin,host FROM mysql.user;
You can exit the MySQL shell:
exit
Now to access MySQL, you must run the following:
mysql -u root -p
Enter password you set and you will enter to MySQL prompt.
Step 3: Installing PHP and Configure Nginx to use the PHP Processor
Nginx is now installed to server your pages. MySQL is installed to store and manage your data. However, you don’t have anything that can control all of that and generate dynamic content. This is where PHP comes.
Since Nginx does not contain native PHP processing like some other webserver, you will need to install
php-fpm
, which stands for “fastCGI process manager”. After, you’ll tell Nginx to pass PHP requests to this software for processing.Install the
php-fpm
module along with additonal helper package php-mysql
which will allow PHP to communicate with your database backend.sudo apt install php-fpm php-mysql
Then you need to make a few configuration changes in order to tell Nginx to use the PHP processor for dynamic content.
This is done on the server block level. To do this, create a new server block configuration file within the
/etc/nginx/sites-available/
directory. sudo vim /etc/nginx/sites-available/web-project-2410
# /etc/nginx/sites-available/web-project-2410 server { listen 80; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name web-project-2410; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; } location ~ /\.ht { deny all; } }
After adding this content, save and close the file. Enable your new server block by creating a symbolic link from your new server block configuration file (in the
/etc/nginx/sites-available/
directory) to the /etc/nginx/sites-enabled/
directory:sudo ln -s /etc/nginx/sites-available/web-project-2410 /etc/nginx/sites-enabled/
Then unlink the default configurationfile from the
/sites-enabled/
directory:sudo unlink /etc/nginx/sites-enabled/default
Test your new configuration file for syntax errors:
sudo nginx -t
Reload the Nginx to make the necessary changes:
sudo systemctl reload nginx
Step 4: Test the Web Server Setup
Our stack should now be completely set up. We can test it to validate that Nginx can correctly handle
.php
files off to the PHP processor.To do this, create a test PHP file called
info.php
in our document root:sudo vim /var/www/html/info.php
Enter the following liens into the new file. This is a valid PHP code that will return information about our server:
# /var/www/html/info.php <?php phpinfo();
Save and close the file. We can now visit this page in our web browser by visting the server’s domain name or public IP address followed by
/info.php
http://<server domain or IP>/info.php
If your can see the page, then you already setup PHP processing with Nginx successfully. Congratulations.
After verifying that Nginx renders the page corretly, it’s best to remove
info.php
to avoid exposing server information.If you get the 502 Bad Gateway
cat /var/log/nginx/error.log
Wrong php fpm version use by Nginx:
Change the nginx.conf
# /etc/nginx/sites-available/web-project-2410 server { listen 80; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name web-project-2410; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; } location ~ /\.ht { deny all; } }
Reload nginx
sudo nginx -s reload
Conclusion
This guidance is about LEMP stack, Linux, Nginx (Engine-X), MySQL, and PHP. This is a powerful platform that allow us to set up an server nearly any website or application.
From now on, we can continue to develop our PHP web application that will guided on following article Create a Dynamic PHP Web Application using MySQL Database