Install php 8 and Apache on Ubuntu 22

Installing Maria DB

Last update: 11-September-2023

You can install Maria DB or other database manager separately.

I invite you to see this manual to install Maria DB.

https://linuxcastle.blogspot.com/2023/08/installing-maria-db-on-ubuntu-22.html 

Install Apache2

Type on console:


    sudo apt-get -y install apache2

 
Up to this point you can check the correct installation of apache go hadead browser and type: http:://localhost 

In this case default port is 80.

Install PHP 8

Type this comand:

   
    sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php 

 
Then it will be displayed as the following image:



Update apt changes

   
    sudo apt update

 

Install PHP vesion 8.1

   
    sudo apt install php8.1

 
Then it will be displayed as the following image:



Type Y for continue:

For verify php type:


   
    php -v

 

Install PHP additional packages

This is an optional step.

If you need to install advanced systems you will need to install some of these packets.

Type this command for install aditional packets.

   
    sudo apt install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl php8.1-gd php8.1-zip php7.0-intl


 
Then it will be displayed as the following image:


Type "Y" to continue.

I recommend restart apache for not restart machine.

   
    sudo systemctl restart apache2


Config Apache publication folder

By default publication folder "/var/www/html" does not have privileges.

Change owner folder.

   
    sudo chown www-data:www-data /var/www/html/


Type for see owner permision:

 
   
    cd /var/www
    ls -l



You need give change folder permission for not require root password into public folder.

   
    sudo chmod 777 /var/www/html/

 
In this image you can see that folder var/www/html change color and observe permissions



You can now create new folder without need root password.

Testing public new sites.

If you followed the steps in the previous point, now you can create a file with the following command

Create a new folder:

   
    mkdir /var/www/html/test

 

Type this command for create a new PHP page using the command "echo".

   
    echo "<?php echo phpinfo();" >  /var/www/html/test/index.php

 
Go browser and type http://localhost/test/ and you should see a page like this:



Configuring Apache Mod_rewrite

This is an optional step.

If you are thinking work with some framework like yii or laravel, you will need configure a modulo for preaty urls.

The Mod_rewrite module is not enabled by default, so it has to be enabled manually, you can follow these steps to do it.


   
   sudo a2enmod rewrite




   
  sudo systemctl restart apache2

Enable access to read .htaccess files

Edit the following file from the console with the command:

   
 sudo nano /etc/apache2/apache2.conf

    

You must locate within the file the section that says:

"<Directory /var/www/>":



You must change within that section where it says 

"AllowOverride None" 
for
"AllowOverride All"

With this change it will be allowed to use the ".htaccess" file for all the sites that are published on the apach server.



Save file using nano with "Ctrl+w" and confirm "Y" and "Enter"
Once changed save the file.

Restart apache to apply the changes.


   
 sudo systemctl restart apache2

    

Cloning new projects into Apache

I recommend doing it this way because if you clone first and just changing the permissions of your project folder inside git will show all the git files as new files. For this reason I recommend following the steps below.

I recomend first create a new folder with permison for write later clone into the folder.

Example:


   
 chmod -R 777 /var/www/html/miprojectfolder

    

Ther clone into folder created "/var/www/html/miprojectfolder":


   
 got clone <yourgit>



If you subfolder into your repository you have to move all content into sub folder to one level up.

   
  mv -f /var/www/html/miprojectfolder/your_git_sub_folder/{.,}* /var/www/html/miprojectfolder/ 

    

If you need give write permissions to folders for example:


   
 chmod -R 777 /var/www/html/miprojectfolder/runtime

    


References:

https://tutoriales-yii2.blogspot.com/2022/08/instalar-apache-php-7.4-mysql-en-ubuntu-22.html

Comments