Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Web Servers»How to Install Apache with PHP-FPM on Ubuntu 22.04

    How to Install Apache with PHP-FPM on Ubuntu 22.04

    By RahulFebruary 26, 20233 Mins Read

    Apache and PHP-FPM are both widely used in web development to serve dynamic content. While Apache is the most popular web server used today, PHP-FPM is a FastCGI process manager that provides a faster and more efficient way to process PHP requests. Combining the two can significantly improve the performance of a website.

    Advertisement

    In this article, we will provide a step-by-step guide on how to install Apache with PHP-FPM on Ubuntu 22.04.

    Step 1: Update your system

    Before installing any new software, you should update your system to ensure that all packages are up to date. You can do this by running the following command in the terminal:

    sudo apt update && sudo apt upgrade 
    

    Step 2: Install Apache

    To install Apache on Ubuntu 22.04, run the following command in the terminal:

    sudo apt install apache2 
    

    Once the installation is complete, you can start the Apache service by running the following command:

    sudo systemctl start apache2 
    

    You can also enable Apache to start at boot time by running the following command:

    sudo systemctl enable apache2 
    

    Step 3: Install PHP-FPM

    The default repositories may not contain the latest PHP version packages. So we always prefer to use ondrej/php PPA for installing PHP that contains most of the versions.

    1. To configure PPA, run the following commands:
      sudo apt install python-software-properties 
      sudo add-apt-repository ppa:ondrej/php 
      
    2. Then install the PHP along with PHP-FPM, by running the following command in the terminal:
      sudo apt install php8.2 php8.2-fpm 
      

      Note: Replace “8.2” with the required PHP versions (Like: 8.1, 7.4, 7.3, etc).

    3. Once the installation is complete, you can start the PHP-FPM service by running the following command:
      sudo systemctl start php8.2-fpm 
      
    4. You can also enable PHP-FPM to start at boot time by running the following command:
      sudo systemctl enable php8.2-fpm 
      

    Step 4: Configure Apache to use PHP-FPM

    1. To configure Apache to use PHP-FPM, you need to enable the “proxy_fcgi” and “proxy” modules. You can do this by running the following command:
      sudo a2enmod proxy_fcgi proxy 
      
    2. Create a new configuration file for your virtual host using the following command:
      sudo nano /etc/apache2/sites-available/example.com.conf 
      

      Note: Replace “example.com” with your domain name.

    3. Inside the new configuration file, add the following configuration with PHP-FPM:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      <VirtualHost *:80>
          ServerName example.com
          ServerAlias www.example.com
          DocumentRoot /var/www/html
          <Directory /var/www/html>
              Options -Indexes +FollowSymLinks +MultiViews
              AllowOverride All
              Require all granted
          </Directory>
          <FilesMatch \.php$>
              SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost/"
          </FilesMatch>
          ErrorLog ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      </VirtualHost>

      Note: Adjust the “ServerName”, “ServerAlias”, “DocumentRoot” directives, and PHP FPM socket path to match your domain and desired file path.

    4. Save and close the file by pressing CTRL+X, then Y and ENTER.
    5. Enable the new virtual host by running the following command:
      sudo a2ensite example.com.conf 
      
    6. Reload the Apache web server to apply the changes:
      sudo systemctl reload apache2 
      

    Step 5: Test the configuration

    To test the Apache with PHP-FPM configuration, create a new PHP file in the document root directory of your website:

    sudo nano /var/www/html/test.php 
    

    Add the following lines to the file:

    1
    <?php phpinfo(); ?>

    Save the file and exit the text editor. Then, open your web browser and navigate to “http://example.com/test.php”. If everything is configured correctly, you should see the PHP information page.

    How to Setup Apache with PHP-FPM on Ubuntu 22.04
    Setting Up Apache with PHP-FPM

    Conclusion

    Installing Apache with PHP-FPM on Ubuntu 22.04 can enhance website performance and overall user experience. Apache is a widely used web server, while PHP-FPM is a FastCGI process manager that enables efficient processing of PHP requests. Together, they provide a powerful combination that can optimize website speed and performance. By following the step-by-step guide in this article, you can easily install Apache with PHP-FPM on Ubuntu 22.04 and take advantage of its benefits.

    Apache2 PHP php-fpm
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Prevent SQL-injection in PHP using Prepared Statements

    Preventing SQL injection attacks with prepared statements in MySQL

    PHP Arrays: A Beginner’s Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Python Lambda Functions – A Beginner’s Guide
    • 10 Practical Use Cases for Lambda Functions in Python
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.