This comprehensive guide outlines the step-by-step process of setting up the Laravel PHP framework on your Ubuntu 20.04 LTS system, ensuring a seamless development experience.
PHP
We’ll verify if PHP is installed on the system by using php -v
command.
We will check the updates and upgrade the system by using the following commands.
sudo apt update sudo apt upgrade
PHP repository
We’ll need to use a third-party repository. We’ll use the repository by Ondřej Surý.
We’ll install the package as follows to add the repositories.sudo apt-get install software-properties-common
Then, we’ll add the PHP repository as below.sudo add-apt-repository ppa:ondrej/php
*Something to note: The repository author has advised the apache2 users to add ppa:ondrej/apache2
We’ll update our package list as below.sudo apt-get update
PHP 8.1
Now, we’ll install PHP 8.1 with the following command:sudo apt-get install php8.1
We can then verify if PHP 8.1 is installed on our server by running the following command:php -v
PHP 8.1 Extensions
Since PHP extensions are libraries that extend the functionality of PHP, we need some extensions for our Laravel project. We’ll install them with the following commands:
sudo apt install php8.1-bcmath sudo apt install php8.1-ctype sudo apt install php8.1-fileinfo sudo apt install php8.1-mbstring sudo apt install openssl sudo apt install php8.1-pdo sudo apt install php8.1-tokenizer sudo apt install php8.1-xml sudo apt install php8.1-zip sudo apt install php8.1-gd
Web Server
We need a web server to host the Laravel application. We can use either Apache or Nginx web server, but we’ll install an Apache web server with the following command:sudo apt install apache2
We’ll then verify if the Apache is running by using the following command:sudo systemctl status apache2
NOTE: We can use the commands sudo systemctl start apache2
to start the Apache, and sudo systemctl enable apache2
to enable the apache to start at boot startup.
Database System
Laravel supports a wide variety of database systems like SQLite, MySQL, Postgres, MariaDB and SQL Server. We’ll choose MySQL as our database system since MySQL is more robust with frequent updates and security improvements.
We’ll install MySQL with the following command:sudo apt install mysql-server
We’ll verify the MySQL installation with the following command:mysql --version
We then need to secure our MySQL user account with password authentication by running the included security script. Before running the script, we need to authenticate the MySQL root user as follows:
- Open the MySQL prompt:
sudo mysql
- Use the following command to change the root user’s authentication method to mysql_native_password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'type_your_password_here';
- Exit the MySQL prompt:
exit
Now, we can install the MySQL security script with the following command:sudo mysql_secure_installation
NOTE: The script will estimate the strength of our password and requires confirmation before it prompts us to configure security features.
We’ll verify MySQL server is running with the following command:sudo systemctl status mysql
NOTE: We might need to pressq
to quit from the status, or we can use the--no-pager
option.
If MySQL isn’t running, we can start it with the following command:sudo systemctl start mysql
At this stage, we’ll change the root user’s authentication method back to the default so that we can once again connect to MySQL as the root user using thesudo mysql
command. - Open the MySQL prompt:
mysql -u root -p
- Change back to the default authentication method:
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
- Exit the MySQL prompt:
exit
Create a MySQL User and Granting Privileges
It is recommended to create a separate database user to connect the database. To create a new user, we’ll open the MySQL prompt as above. Then, we’ll use the following syntax:CREATE USER 'type_your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'type_your_password';
We can also create a database on MySQL, then create a user and assign privileges to the user on that particular database. In our case, we want to grant our MySQL user the ALL PRIVILEGES to have complete control over every database on the server. So, we will use the following syntax:GRANT ALL PRIVILEGES ON *.* TO 'type_your_username'@'localhost' WITH GRANT OPTION;
Lastly, we’ll free up the server-cached memory with the following syntax:FLUSH PRIVILEGES;
phpMyAdmin
We’ll install a secure phpMyAdmin to manage our databases through a web interface safely.
Firstly, we need to disable the Validate Password component from MySQL with the following syntax in the MySQL prompt:UNINSTALL COMPONENT "file://component_validate_password";
Then, exit the MySQL prompt and use the following command to install phpMyAdmin:sudo apt install phpmyadmin
When a prompt appears while installing, we need to configure the package as below:
- For the server selection, we’ll choose apache2
IMPORTANT: We need to hit SPACE, TAB, and then ENTER to select Apache. Otherwise, the installer will not move the necessary files during installation. - For the dbconfig-common to set up the database, we’ll select Yes
Then, we need to confirm a MySQL application password for phpMyAdmin.
After phpMyAdmin is installed, we’ll open the MySQL prompt once again to enable the Validate Password component with the following syntax:INSTALL COMPONENT "file://component_validate_password";
We’ll use the following command to enable the mbstring PHP extension for Apache and PHP to work with phpMyAdmin:sudo phpenmod mbstring
Finally, we’ll restart the Apache with the following command in order for the changes to take effect:sudo systemctl restart apache2
Theoretically, we should now be able to access the web interface by visiting localhost or public IP address followed by /phpmyadmin. But, we get a 404 error upon visiting http://localhost/phpmyadmin. Because we need to include the following line at the bottom of the /etc/apache2/apache2.conf file:Include /etc/phpmyadmin/apache.conf
Now we can access the phpMyAdmin web interface on http://localhost/phpmyadmin, though we’re getting a series of depreciation errors. Because we’re using PHP 8 and earlier versions of phpMyAdmin 5. So, we need to upgrade our phpMyAdmin to version 5. To do so, we’ll back up our existing phpMyAdmin by using the following command:sudo mv /usr/share/phpmyadmin/ /usr/share/phpmyadmin.bak
And we’ll make a directory for phpMyAdmin as follows:sudo mkdir /usr/share/phpmyadmin/
We then get inside the directory:cd /usr/share/phpmyadmin/
We’ll download the latest version of phpMyAdmin. As of July 2022, we have version 5.2.0 is the latest, so we’ll use the following command:sudo wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.tar.gz
After the file download, we’ll unzip the file with the following command:sudo tar xzf phpMyAdmin-5.2.0-all-languages.tar.gz
And we’ll use the following command to move the files:sudo mv phpMyAdmin-5.2.0-all-languages/* /usr/share/phpmyadmin
Now, our phpMyAdmin is upgraded to version 5.2, and the depreciation error has disappeared. But we’re getting a new error once we log into our phpMyAdmin web interface.
The first error is about the configuration file needing a secret passphrase (blowfish_secret).
To resolve this error, we’ll follow the steps below:
- Create a configuration file inside phpMyAdmin folder:
sudo nano /usr/share/phpmyadmin/config.inc.php
- Generate our unique 32-character blowfish secret from this link:
https://passgen.co/?pw=32&a=1 - Write the following code and save the configuration file:
<?php // use here a value of your choice 32 chars long $cfg['blowfish_secret'] = 'UNIQUE__32__CHAR__BLOWFISH_SECRET'; $i=0; $i++; $cfg['Servers'][$i]['auth_type'] = 'cookie';
The second error says $cfg‘TempDir’ is not accessible.
To resolve this error, we’ll simply create the required directory and make it writable as below.sudo mkdir /usr/share/phpmyadmin/tmp && sudo chmod 777 /usr/share/phpmyadmin/tmp
We’re now good to go with our phpMyAdmin.
Composer
We’ll need to install Composer dependency package manager for PHP to use Laravel.
So, we’ll use the following command:scurl -sS https://getcomposer.org/installer | php
After the installation, we’ll move the installed file as follows:sudo mv composer.phar /usr/bin/composer
Then, we’ll assign the permission as follows:sudo chmod +x /usr/bin/composer
Just to make sure, we’ll check our installation with the command below:composer --version
Laravel
Now, we’re ready to install Laravel. Let’s navigate to the webroot directory:cd /var/www/html
We’ll install Laravel using the composer command as below:sudo composer create-project laravel/laravel your_project_name
NOTE: If we run the above command from the root, we will receive a notice that reads – “Do not run Composer as root/super user! See https://getcomposer.org/root for details”. But we’ll run as a root user anyway. Because the notice is only a warning since it’s dangerous to run composer as root user. The notice is, in fact, to inform us that there could be malicious code that gets executed with our installation and has full rights on our system.
We then need to change the ownership of the Laravel directory with the following command:sudo chown -R www-data:www-data /var/www/html/your_project_name
And we’ll also change the permissions as below:sudo chmod -R 775 /var/www/html/your_project_name/storage
Configuration
Now, we need to configure Apache to serve the Laravel site. Let’s use “test.localhost” as our domain name for the Laravel site.
First, we’ll create a virtual host file with the following command:sudo nano /etc/apache2/sites-available/your_project_name.conf
Second, we’ll write the following code:
<VirtualHost *:80> ServerName test.localhost ServerAdmin admin@test.localhost DocumentRoot /var/www/html/your_project_name/public <Directory /var/www/html/your_project_name> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
and save the changes, then exit.
Third, we’ll enable the Laravel site with the following command:sudo a2ensite your_project_name.conf
Fourth, we’ll enable the Apache rewrite module using the following command:sudo a2enmod rewrite
Finally, we’ll restart Apache to apply the changes below:sudo systemctl restart apache2
At this stage, we can access our Laravel site on a web browser by going to test.localhost, and we’ll see the default Laravel page.
We’ve now successfully installed the Laravel PHP Framework on Ubuntu 20.04 LTS systems.
References:
Drake, M., 2020. How To Install and Secure phpMyAdmin on Ubuntu 20.04 | DigitalOcean. [online] Digitalocean.com. Available at: https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-20-04 [Accessed 18 July 2022].
DevAnswers.co. 2022. How to Manually Upgrade phpMyAdmin – DevAnswers.co. [online] Available at: https://devanswers.co/manually-upgrade-phpmyadmin/ [Accessed 18 July 2022].
Marijan, B., 2021. How to Install MySQL on Ubuntu 20.04 {5-Step Process}. [online] Knowledge Base by phoenixNAP. Available at: https://phoenixnap.com/kb/install-mysql-ubuntu-20-04 [Accessed 18 July 2022].
Virdó, H. and Drake, M., 2022. How To Install MySQL on Ubuntu 20.04 | DigitalOcean. [online] Digitalocean.com. Available at: https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04 [Accessed 18 July 2022].
Leave a Reply