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

Cyberac1d
0

 


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:

<FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://localhost/" </FilesMatch>
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

<?php phpinfo(); ?>

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.

Post a Comment

0Comments

Thanks for sharing your feedback! It helps us grow.

Post a Comment (0)

Contact form