App Web Tutorials
  • Home
  • Contact
  • Privacy Policy
Reading: The Ultimate Step-by-Step Guide to Setting Up an Ubuntu Server with Apache2, PHP-FPM, MySQL, and HTTPS
Share
Subscribe Now
App Web TutorialsApp Web Tutorials
Font ResizerAa
Search
  • Home
  • Contact
  • Privacy Policy
Follow US
Copyright © 2017-2025 App Web Tutorials. All Rights Reserved.
Apache2MySQLPHP

The Ultimate Step-by-Step Guide to Setting Up an Ubuntu Server with Apache2, PHP-FPM, MySQL, and HTTPS

Chetan
By Chetan
Last updated: January 20, 2025
5 Min Read
Step-by-Step Guide: Setting Up an Ubuntu Server with Apache2, PHP-FPM, MySQL, and HTTPS
Step-by-Step Guide: Setting Up an Ubuntu Server with Apache2, PHP-FPM, MySQL, and HTTPS
SHARE

Setting up a robust and secure web server is crucial for hosting PHP applications. In this tutorial, we’ll walk you through the process of setting up an Ubuntu server with Apache2, PHP-FPM, MySQL, and HTTPS. We’ll also configure virtual hosts, secure the server with SSL using certbot, and optimize the server for running PHP code.

Prerequisites:

  • A fresh installation of Ubuntu Server
  • Root access or a user with sudo privileges

Step 1: Update the System
Begin by updating your Ubuntu server to ensure you have the latest security patches and software updates.

sudo apt-get update
sudo apt-get upgrade

Step 2: Install Apache2
Install the Apache2 web server using the following command:

sudo apt-get install apache2

Step 3: Install PHP and PHP-FPM
Install PHP and PHP-FPM (FastCGI Process Manager) to handle PHP processing.

sudo apt-get install php php-fpm

Step 4: Configure Apache2 to Use PHP-FPM
Enable the necessary Apache modules and configure Apache to use PHP-FPM.

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-fpm

Step 5: Install MySQL
Install the MySQL database server to store and manage your application’s data.

sudo apt-get install mysql-server

Step 6: Secure MySQL Installation
Run the MySQL secure installation script to set a root password and improve the security of your MySQL installation.

sudo mysql_secure_installation

Step 7: Create a Virtual Host
Set up a virtual host for your PHP application. Create a new configuration file in the /etc/apache2/sites-available directory.

sudo nano /etc/apache2/sites-available/your-domain.com.conf

Add the following configuration:

<VirtualHost *:80> 
ServerName your-domain.com
ServerAlias www.your-domain.com 
DocumentRoot /var/www/your-domain.com 
ErrorLog ${APACHE_LOG_DIR}/error.log 
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/your-domain.com>
AllowOverride All 
Require all granted 
</Directory> 
</VirtualHost>
<VirtualHost *:443> 
ServerName your-domain.com 
ServerAlias www.your-domain.com 
DocumentRoot /var/www/your-domain.com 
<Directory /var/www/your-domain.com>
 Options Indexes 
FollowSymLinks MultiViews 
AllowOverride All 
Require all granted 
</Directory> 
SSLEngine on 
SSLCertificateFile /etc/letsencrypt/live/your-domain.com/fullchain.pem 
SSLCertificateKeyFile /etc/letsencrypt/live/your-domain.com/privkey.pem 
Include /etc/letsencrypt/options-ssl-apache.conf 
</VirtualHost>

Enable the virtual host:

sudo a2ensite your-domain.com.conf

Step 8: Configure SSL with Certbot
Install certbot and obtain an SSL certificate for your domain to enable HTTPS.

sudo apt-get install certbot python3-certbot-apache
sudo certbot --apache -d your-domain.com -d www.your-domain.com

Step 9: Configure HTTP/2 Enable the http2 module in Apache and configure your virtual host to use HTTP/2.

sudo a2enmod http2
sudo a2enmod ssl 
sudo a2dismod php8.3
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event

Configure Apache to use php-fpm by enabling the proxy_fcgi module and the setenvif module:

sudo a2enmod proxy_fcgi setenvif

Create a new Apache configuration file for PHP-FPM:

sudo nano /etc/apache2/conf-available/php8.3-fpm.conf

Add the following lines to the file:

    
        SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"

Enable the new configuration file:

sudo a2enconf php8.3-fpm

Restart Apache and PHP-FPM::

sudo systemctl restart apache2 sudo systemctl restart php8.3-fpm

Modify your virtual host configuration file to include the necessary directives for HTTP/2 and SSL.

Step 10: Optimize PHP-FPM
Adjust the PHP-FPM configuration to optimize performance based on your server’s resources and application requirements. Modify the /etc/php/8.3/fpm/pool.d/www.conf file to tune settings like pm.max_children, pm.start_servers, pm.min_spare_servers, and pm.max_spare_servers.

Step 11: Restart Services
Restart Apache and PHP-FPM to apply the changes.

sudo systemctl restart apache2
sudo systemctl restart php8.3-fpm

Step 12: Test Your Setup
Create a PHP file in your virtual host’s document root to test the setup.

php

<span style="color: #e06c75;"><?php</span>
<span style="color: #61afef;">phpinfo</span>();
<span style="color: #e06c75;">?></span>

Access the file through your browser using both HTTP and HTTPS to ensure everything is working correctly.

Conclusion:
Congratulations! You have successfully set up an Ubuntu server with Apache2, PHP-FPM, MySQL, and HTTPS. You have also configured virtual hosts, secured your server with SSL using certbot, and optimized your server for running PHP applications. With this setup, you can now deploy your PHP applications with confidence, knowing that your server is reliable, secure, and optimized for performance.

Remember to regularly update your server, monitor logs, and follow security best practices to maintain a robust and secure environment for your PHP applications.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Easily Share Your Localhost Website with the World: Step-by-Step Instructions
Next Article Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

FacebookLike
XFollow
InstagramFollow
Most Popular
How to Install and Use pyenv on Windows, Ubuntu, and macOS: A Complete Guide
The Ultimate Guide to Installing pyenv on Windows, Ubuntu, and macOS
January 21, 2025
How to Host a Python Flask App with SSL on WAMP Server in Windows: A Step-by-Step Guide for Beginners
January 21, 2025
Fixing SSH ‘Unprotected Private Key File’ Error in Windows 11: A Quick Guide
Easily Fix the SSH ‘Unprotected Private Key File’ Error on Windows 11 in Minutes
January 20, 2025
How to Add Eye-Catching Animations to Your HTML Website: A Step-by-Step Guide for Beginners
Power Your HTML Website with Stunning Animations: Easy Guide for Beginners
January 15, 2025
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
January 21, 2025

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!
App Web Tutorials

We provide tutorials, tips, tricks, and advice for improving programming and development skills.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?