DEV Community

medilies
medilies

Posted on

Run Laravel locally on Ubuntu using Apache virtual host

Refresh APT metadata.

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Installing MySQL

Install MySQL:

sudo apt install mysql-server
Enter fullscreen mode Exit fullscreen mode

Enter MySQL to edit the root user password:

sudo mysql
Enter fullscreen mode Exit fullscreen mode
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Enter fullscreen mode Exit fullscreen mode

Re-enter MySQL to create a database for our app, but using the credentials this time:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode
CREATE DATABASE my_app;
Enter fullscreen mode Exit fullscreen mode

References:

Installing Apache

Ubuntu usually comes bundled with a running Apache server but still, I'll include the installation steps.

sudo apt-get install apache2 -y
Enter fullscreen mode Exit fullscreen mode

Check if Apache is installed by verifying its version:

apache2ctl -v
Enter fullscreen mode Exit fullscreen mode

Check if your firewall is active:

sudo ufw status
sudo ufw app list
Enter fullscreen mode Exit fullscreen mode

If it returned Status: active then allow HTTP traffic on Apache:

sudo ufw allow in "Apache"
Enter fullscreen mode Exit fullscreen mode

Now, visiting http://localhost should display "Apache2 Default Page".

References:

Installing PHP

Register the following repo that enables the installation of multiple PHP versions at once.

sudo add-apt-repository ppa:ondrej/php
Enter fullscreen mode Exit fullscreen mode

Install PHP 8.1.

sudo apt install php8.1
Enter fullscreen mode Exit fullscreen mode

If you opt for a different version then just replace 8.1 with your version whenever you copy a command.

Add PHP module to Apache server:

sudo apt install libapache2-mod-php8.1
Enter fullscreen mode Exit fullscreen mode

Install the extensions required by Laravel:

sudo apt install php8.1-mbstring php8.1-xmlrpc php8.1-soap php8.1-gd php8.1-xml php8.1-cli php8.1-zip php8.1-bcmath php8.1-tokenizer php8.1-json php8.1-pear
Enter fullscreen mode Exit fullscreen mode

The newly installed extensions will be automatically enabled with their configs placed at /etc/php/8.1/cli/conf.d/.

References:

Installing composer

Check https://getcomposer.org/download/.

A new Laravel app

cd
mkdir dev
cd dev
Enter fullscreen mode Exit fullscreen mode

I like to place my code at ~/dev.

Create a new Laravel app:

composer global require laravel/installer
laravel new my_app --git
Enter fullscreen mode Exit fullscreen mode

Set the correct permissions to enable Apache to execute your PHP code.

sudo chown -R www-data:www-data /home/me/dev/my_app
sudo chmod -R 777 /home/me/dev/my_app
sudo chmod o+x /home/me
sudo chmod o+x /home/me/dev
sudo chmod o+x /home/me/dev/
sudo chmod o+x /home/me/dev/my_app
Enter fullscreen mode Exit fullscreen mode

Set .env with your app and database details.

Faking a domain name

We will add a domain name for our app that only our machine knows about by editing /etc/hosts. We will configure it to let our machine know that the domain name my_app.local is on the loopback IP address 127.0.0.1.

nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode
# Add this line anywhere
127.0.0.1 my_app.local
Enter fullscreen mode Exit fullscreen mode

Test that you configured the domain correctly by pinging it:

ping my_app.local
Enter fullscreen mode Exit fullscreen mode

Note that using internet top-level domains like .com will most likely not work. So I recommend sticking to .local for testing locally without HTTPS and without a registered domain name.

Setting up the virtual host

Now we move to /etc/apache2/sites-available/ and use as our base virtual host config file:

cd /etc/apache2/sites-available/
sudo cp 000-default.conf my_app-local.conf
nano my_app-local.conf
Enter fullscreen mode Exit fullscreen mode

And edit my_app-local.conf to look like this:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName my_app.local   
        ServerAlias www.my_app.local

        ServerAdmin webmaster@localhost
        DocumentRoot /home/me/dev/my_app/public

        <Directory /home/me/dev/my_app/public>
                Options FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>

        ReWriteEngine On

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Check the validity of the config you added:

sudo apache2ctl configtest
Enter fullscreen mode Exit fullscreen mode

Enable your new site:

sudo a2ensite my_app-local.conf
Enter fullscreen mode Exit fullscreen mode

Also, enable the rewrite module to be able to have URLs that do not point only to real files:

sudo a2enmod rewrite
Enter fullscreen mode Exit fullscreen mode

The final thing to do is restart Apache to reload the new config:

sudo systemctl restart apache2
// or
sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

References:

Finally

Visit http://my_app.local.

Top comments (4)

Collapse
 
ghulam_hyderghaloo_c8002 profile image
Ghulam Hyder Ghaloo

Running Laravel locally on Ubuntu using an Apache virtual host involves several steps. First, install Apache, PHP, and MySQL. Next, configure Apache to serve your Laravel project by creating a virtual host file. Enable the site and restart Apache. Finally, update your hosts file to point to your Laravel project's local domain. To promote this knowledge, distribute promotional pens branded with your company's logo and a link to a detailed setup guide.

Collapse
 
medilies profile image
medilies

For dev purposes:

  • If you want to connect to MySQL from any machine do sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf and set bind-address = 0.0.0.0.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.