Laravel is a renowned and open-source PHP web framework developed by Taylor Otwell. It builds upon Symfony and adopts the model–view–controller architectural pattern, empowering developers to create sophisticated web applications with effortless elegance. Laravel’s built-in features simplify and accelerate web app development. The framework gained immense popularity with the release of version 3, which introduced essential functionalities like the Artisan command line, extensive database support, and a packaging system known as bundles.
This guide will walk you through the process of installing the Laravel PHP Framework with the Nginx web server on Ubuntu 22.04.
Prerequisites
- A server running Ubuntu 22.04.
- A valid domain name pointed to your server’s IP address.
- A root password configured on the server.
Install LEMP Server
To get started, you’ll need to install the Nginx web server, the MariaDB database system, PHP, and other essential dependencies on your server. Execute the following command to install them:
apt install -y nginx mariadb-server php php-fpm php-common php-cli php-gd php-mysqlnd php-curl php-intl php-mbstring php-bcmath php-xml php-zip wget git
Once all the packages are installed, verify the PHP version using the command:
php -v
You should see an output similar to this:
PHP 8.1.2 (cli) (built: Apr 7 2022 17:46:26) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
Install PHP Composer
Composer is a vital dependency manager for PHP. To install Composer, first, ensure the curl package is available on your server:
apt install -y curl
Then, install PHP Composer using the command:
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
Once Composer is successfully installed, the output will look like this:
All settings correct for using Composer Downloading... Composer (version 2.3.5) successfully installed to: /usr/bin/composer Use it: php /usr/bin/composer
Verify the Composer version by running:
composer --version
The expected output should be:
Composer version 2.3.5 2022-04-13 16:43:00
Install Laravel on Ubuntu 22.04
Begin by navigating to the Nginx web root directory and downloading the latest version of Laravel using Composer:
cd /var/www/html composer create-project laravel/laravel laravel
The process should produce the following output:
55 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: laravel/sail Discovered Package: laravel/sanctum Discovered Package: laravel/tinker Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Discovered Package: spatie/laravel-ignition Package manifest generated successfully. 78 packages you are using are looking for funding. Use the `composer fund` command to find out more! > @php artisan vendor:publish --tag=laravel-assets --ansi --force No publishable resources for tag [laravel-assets]. Publishing complete. > @php artisan key:generate --ansi Application key set successfully.
Then, switch to the Laravel directory and start the Laravel development server:
cd laravel php artisan serve --host 0.0.0.0 --port 8000
If everything is set up correctly, you’ll see:
Starting Laravel development server: http://0.0.0.0:8000 [Sun May 22 08:17:45 2022] PHP 8.1.2 Development Server (http://0.0.0.0:8000) started
Press CTRL+C to stop the Laravel server. After that, update the ownership and permissions of the Laravel directory:
chown -R www-data:www-data /var/www/html/laravel chmod -R 0777 /var/www/html/laravel
Configure Nginx for Laravel
Create an Nginx virtual host configuration file for Laravel:
nano /etc/nginx/conf.d/laravel.conf
Add the following configuration:
server { listen 80; server_name laravel.example.com; root /var/www/html/laravel/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } }
Save and close the file. Verify the Nginx configuration for any syntax errors with:
nginx -t
The output should confirm:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart the Nginx and PHP-FPM services to apply the changes:
systemctl restart php8.1-fpm nginx
Check the Nginx status using:
systemctl status nginx
The output will be:
? nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2022-05-22 08:19:20 UTC; 17s ago Docs: man:nginx(8) Process: 16865 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 16866 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 16867 (nginx) Tasks: 2 (limit: 2292) Memory: 2.6M CPU: 33ms CGroup: /system.slice/nginx.service ??16867 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ??16868 "nginx: worker process" May 22 08:19:20 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server... May 22 08:19:20 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
Access Laravel Web Interface
Laravel is now installed and configured with Nginx. Access the Laravel Web UI by navigating to http://laravel.example.com. You should be greeted with the Laravel Dashboard as shown below:
Secure Laravel with Let’s Encrypt
To secure your Laravel application, install the Certbot client package for Let’s Encrypt SSL management:
First, get the Certbot package with:
apt-get install certbot python3-certbot-nginx -y
After installation, use Certbot to enable SSL on your domain:
certbot --nginx -d laravel.example.com
Follow the prompts to provide a valid email address and accept the terms of service:
... Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): your-email@example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A ...
You will next choose whether to redirect HTTP traffic to HTTPS:
... 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Type 2 for redirection and press Enter. The final output will confirm success:
... Congratulations! You have successfully enabled https://laravel.example.com ...
Conclusion
Congratulations! You have successfully set up Laravel with Nginx on Ubuntu 22.04. You are now ready to build high-performance PHP applications using the Laravel framework. Feel free to reach out if you have any questions.
FAQ
Q1: Why should I use Laravel for my PHP projects?
A1: Laravel provides an expressive and elegant syntax, offers a comprehensive ecosystem, and facilitates rapid application development with tools like the Artisan command line.
Q2: Can I use this setup with a different Linux distribution?
A2: While this tutorial is specifically for Ubuntu 22.04, the steps are similar for other Debian-based systems. Some commands might slightly differ for other distributions like CentOS or Fedora.
Q3: What if I face issues starting the Laravel server?
A3: Ensure your server’s firewall isn’t blocking required ports and that all necessary packages are installed correctly. You can also check Laravel’s logs for specific error details.
Q4: How do I update Laravel to the latest version?
A4: You can update Laravel using Composer with the command composer update
. Be sure to check Laravel’s official documentation for specific update instructions.