diff --git a/docs/guides/development/frameworks/laravel/_index.md b/docs/guides/development/frameworks/laravel/_index.md index 8ee8675d8fe..22948ea89d9 100644 --- a/docs/guides/development/frameworks/laravel/_index.md +++ b/docs/guides/development/frameworks/laravel/_index.md @@ -7,5 +7,4 @@ published: 2021-06-03 keywords: ["laravel", "php"] license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)' show_in_lists: true ---- - +--- \ No newline at end of file diff --git a/docs/guides/development/frameworks/laravel/how-to-create-website-using-laravel/index.md b/docs/guides/development/frameworks/laravel/how-to-create-website-using-laravel/index.md index 2fc0a326c21..876f4977fa8 100644 --- a/docs/guides/development/frameworks/laravel/how-to-create-website-using-laravel/index.md +++ b/docs/guides/development/frameworks/laravel/how-to-create-website-using-laravel/index.md @@ -6,6 +6,7 @@ description: 'Learn the basics of building a website with the Laravel framework, authors: ["Nathaniel Stickman"] contributors: ["Nathaniel Stickman"] published: 2021-06-04 +modified: 2021-11-07 keywords: ['laravel','php','web application','web framework','deploy a website','debian','ubuntu','centos'] tags: ['laravel', 'debian', 'ubuntu', 'centos', 'php'] license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)' @@ -36,13 +37,13 @@ This guide is written for non-root user. Commands that require elevated privileg ### Install the Prerequisites -1. Install PHP and Laravel's recommended PHP extensions. +1. Install PHP and Laravel's recommended PHP extensions. - - On Debian and Ubuntu, you can use: + - **Debian and Ubuntu**: sudo apt install php php-bcmath php-common php-curl php-json php-mbstring php-mysql php-xml php-zip php8.1-fpm openssl - - On CentOS, you need to take the additional step of adding the [Remi repository](https://rpms.remirepo.net/), since the package manager's default repositories only include PHP version 7.2. + - **CentOS 8**. Since the package manager's default repositories only include PHP version 7.2, you need to take the additional step of adding the [Remi repository](https://rpms.remirepo.net/). - First, add the Remi repository: @@ -58,11 +59,15 @@ This guide is written for non-root user. Commands that require elevated privileg sudo dnf install php php-bcmath php-common php-json php-mbstring php-mysql php-xml php-zip curl openssl -1. Change into the directory where you intend to keep your Laravel project's directory. In this example, you use the current user's home directory. + - **openSUSE**: + + sudo zypper install php7 php7-bcmath php7-curl php7-fileinfo php7-json php7-mbstring php7-mysql php7-openssl php7-zip php-composer + +1. Change into the directory where you intend to keep your Laravel project's directory. In this example, you use the current user's home directory. cd ~ -1. Download [Composer](https://getcomposer.org/), ensure that Composer can be used globally, and make it executable. +1. Download [Composer](https://getcomposer.org/), ensure that Composer can be used globally, and make it executable. curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer @@ -70,30 +75,30 @@ This guide is written for non-root user. Commands that require elevated privileg ### Create a Laravel Project -1. Create your Laravel application. +1. Create your Laravel application. composer create-project laravel/laravel example-app -1. Change into the directory created for the application. +1. Change into the directory created for the application. cd example-app - {{< note respectIndent=false >}} -Unless noted otherwise, all subsequent commands in this guide assume you are still in `example-app` project directory. -{{< /note >}} + {{< note >}} + Unless noted otherwise, all subsequent commands in this guide assume you are still in `example-app` project directory. + {{< /note >}} -1. Run the PHP development server, Artisan, to verify that the Laravel setup is complete. +1. Run the PHP development server, Artisan, to verify that the Laravel setup is complete. php artisan serve Artisan serves the application on `localhost:8000`. To visit the application remotely, you can use an SSH tunnel: - - On Windows, you can use the PuTTY tool to set up your SSH tunnel. Follow the appropriate section of the [Using SSH on Windows](/docs/guides/connect-to-server-over-ssh-on-windows/#ssh-tunnelingport-forwarding) guide, replacing the example port number there with **8000**. - - On OS X or Linux, use the example command to set up the SSH tunnel. Replace `example-user` with your username on the application server and `192.0.2.0` with the server's IP address. Ensure that you can access the server on port `8000` use the `sudo ufw allow 8000` to be enable access. + - On Windows, you can use the PuTTY tool to set up your SSH tunnel. Follow the appropriate section of the [Using SSH on Windows](/docs/guides/connect-to-server-over-ssh-on-windows/#ssh-tunnelingport-forwarding) guide, replacing the example port number there with **8000**. + - On OS X or Linux, use the example command to set up the SSH tunnel. Replace `example-user` with your username on the application server and `192.0.2.0` with the server's IP address. Ensure that you can access the server on port `8000` use the `sudo ufw allow 8000` to be enable access. ssh -L8000:localhost:8000 example-user@192.0.2.0 -1. Now, you can visit the application in your browser by navigating to `localhost:8000`. +1. Now, you can visit the application in your browser by navigating to `localhost:8000`. ![Laravel base application](laravel-base-app.png) @@ -101,115 +106,115 @@ Unless noted otherwise, all subsequent commands in this guide assume you are sti This section shows you how to start working with Laravel's *controllers* and *views* to make your own website. -1. Follow the steps in the [Create a Laravel Project](#create-a-laravel-project) section above to get started with a base project. +1. Follow the steps in the [Create a Laravel Project](#create-a-laravel-project) section above to get started with a base project. -1. This example builds a website with a **Home** page and an **About** page. Create the routes for each by opening the routes file — `~/example-app/routes/web.php` — and add the following contents: +1. This example builds a website with a **Home** page and an **About** page. Create the routes for each by opening the routes file — `~/example-app/routes/web.php` — and add the following contents: - {{< file "~/example-app/routes/web.php" >}} -}} + Route::get('/about', [AboutController::class, 'index']); + ``` First, this imports the controllers—`HomeController` and `AboutController` that get created in the next two steps. Then, it routes requests to the `/home` and `/about` URLs to their respective controllers. It also includes a route to redirect traffic from the base URL (`/`) to the `/home` URL. -1. Create the Home controller by creating an `~/example-app/app/Http/Controllers/HomeController.php` file and giving it the contents shown below: +1. Create the Home controller by creating an `~/example-app/app/Http/Controllers/HomeController.php` file and giving it the contents shown below: - {{< file "~/example-app/app/Http/Controllers/HomeController.php" >}} - 'Home Page']); + public function index() + { + return view('home', ['title' => 'Home Page']); + } } -} - {{< /file >}} + ``` This controller simply renders the Home page view and feeds a `title` parameter into it. -1. Do the same for the About controller. In this case, the new file is `~/example-app/app/Http/Controllers/AboutController.php`. This controller serves the same function as the Home controller, however, it renders the about page view instead. +1. Do the same for the About controller. In this case, the new file is `~/example-app/app/Http/Controllers/AboutController.php`. This controller serves the same function as the Home controller, however, it renders the about page view instead. - {{< file "~/example-app/app/Http/Controllers/AboutController.php" >}} - 'About Page']); + public function index() + { + return view('about', ['title' => 'About Page']); + } } -} - {{< /file >}} - -1. This example's views share a navigation menu, so the website can use a layout template to reduce duplicate code. Create the layout template as `~/example-app/resources/views/layouts/master.blade.php`, and give it the contents shown in the example below. - - {{< note respectIndent=false >}} -Before creating your layout template, you need to create the `layouts` subdirectory. - - mkdir ~/example-app/resources/views/layouts -{{< /note >}} - - {{< file "~/example-app/resources/views/layouts/master.blade.php" >}} - - - @if ($title) - {{ $title }} - @else - Example Laravel App - @endif - - -
Home | About -
-
- @yield('content') -
- - - {{< /file >}} - -1. Now, to create the views themselves. Create a `~/example-app/resources/views/home.blade.php` file and a `~/example-app/resources/views/about.blade.php` file. Add the contents of the example files below: - - {{< file "~/example-app/resources/views/home.blade.php" >}} -@extends('layouts.master') - -@section('content') -

{{ $title }}

-

This is the home page for an example Laravel web application.

-@endsection - {{< /file >}} - - {{< file "~/example-app/resources/views/about.blade.php" >}} -@extends('layouts.master') - -@section('content') -

{{ $title }}

-

This is the about page for an example Laravel web application.

-@endsection - {{< /file >}} + ``` + +1. This example's views share a navigation menu, so the website can use a layout template to reduce duplicate code. Create the layout template as `~/example-app/resources/views/layouts/master.blade.php`, and give it the contents shown in the example below. + + {{< note >}} + Before creating your layout template, you need to create the `layouts` subdirectory. + + mkdir ~/example-app/resources/views/layouts + {{< /note >}} + + ```file {title="~/example-app/resources/views/layouts/master.blade.php"} + + + @if ($title) + {{ $title }} + @else + Example Laravel App + @endif + + +
Home | About +
+
+ @yield('content') +
+ + + ``` + +1. Now, to create the views themselves. Create a `~/example-app/resources/views/home.blade.php` file and a `~/example-app/resources/views/about.blade.php` file. Add the contents of the example files below: + + ```file {title="~/example-app/resources/views/home.blade.php"} + @extends('layouts.master') + + @section('content') +

{{ $title }}

+

This is the home page for an example Laravel web application.

+ @endsection + ``` + + ```file {title="~/example-app/resources/views/about.blade.php"} + @extends('layouts.master') + + @section('content') +

{{ $title }}

+

This is the about page for an example Laravel web application.

+ @endsection + ``` Each of these view templates first declares that it extends the `master` layout template. This lets each work within the layout, reducing the amount of code you have to rewrite and making sure the pages are consistent. Each view defines its main contents as being part of the `content` section, which was defined in the `master` layout. -1. Run the application using the steps given at the end of the [Create a Laravel Project](#create-a-laravel-project) section above. +1. Run the application using the steps given at the end of the [Create a Laravel Project](#create-a-laravel-project) section above. You can now visit the website on `localhost:8000`. @@ -221,88 +226,92 @@ While the Artisan server works well for development, it is recommended that you These steps assume your application has the same location and name as given in the previous sections. -1. Install NGINX. +1. Install NGINX and php-fpm. + + - On Debian and Ubuntu, use: - - On Debian and Ubuntu, use: + sudo apt install nginx php7.4-fpm - sudo apt install nginx + - On CentOS, use: - - On CentOS, use: + sudo yum install nginx php-fpm - sudo yum install nginx + - On openSUSE, use: -1. Copy your Laravel project directory to `/var/www`. + sudo zypper install nginx php-fpm + +1. Copy your Laravel project directory to `/var/www`. sudo cp -R ~/example-app /var/www -1. Give the `www-data` user ownership of the project's `storage` subdirectory. +1. Give the `www-data` user ownership of the project's `storage` subdirectory. sudo chown -R www-data.www-data /var/www/example-app/storage -1. Create an NGINX configuration file for the website, and add the contents shown below. Replace `example.com` with your server's domain name. +1. Create an NGINX configuration file for the website, and add the contents shown below. Replace `example.com` with your server's domain name. - {{< file "/etc/nginx/sites-available/example-app" >}} -server { - listen 80; - server_name example.com; - root /var/www/example-app/public; + ```file {title="/etc/nginx/sites-available/example-app"} + server { + listen 80; + server_name example.com; + root /var/www/example-app/public; - add_header X-Frame-Options "SAMEORIGIN"; - add_header X-Content-Type-Options "nosniff"; + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-Content-Type-Options "nosniff"; - index index.php; + index index.php; - charset utf-8; + charset utf-8; - location / { - try_files $uri $uri/ /index.php?$query_string; - } + 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; } + 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; + error_page 404 /index.php; - location ~ \.php$ { - fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; - fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; - include fastcgi_params; - } + location ~ \.php$ { + fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; + fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; + include fastcgi_params; + } - location ~ /\.(?!well-known).* { - deny all; + location ~ /\.(?!well-known).* { + deny all; + } } -} - {{< /file >}} + ``` -1. Create a symbolic link of the configuration file in the NGINX `sites-enabled` directory. You can also remove the `default` site configuration from this directory. +1. Create a symbolic link of the configuration file in the NGINX `sites-enabled` directory. You can also remove the `default` site configuration from this directory. sudo ln -s /etc/nginx/sites-available/example-app /etc/nginx/sites-enabled/ sudo rm /etc/nginx/sites-enabled/default -1. Verify the NGINX configuration. +1. Verify the NGINX configuration. sudo nginx -t -1. Enable and start the NGINX service. +1. Enable and start the NGINX service. sudo systemctl enable nginx sudo systemctl start nginx -1. Similarly, enable and start the PHP-FPM service, which NGINX uses to run your application. +1. Similarly, enable and start the PHP-FPM service, which NGINX uses to run your application. - - On Debian and Ubuntu, use: + - On Debian and Ubuntu, use: sudo systemctl enable php7.4-fpm sudo systemctl start php7.4-fpm - - On CentOS, use: + - On CentOS, use: sudo systemctl enable php-fpm sudo systemctl start php-fpm -1. Your application should now be running — visit it by navigating to your server's domain name in your browser. Make sure you prefix your domain name with `http` rather than `https`, as the server has not been set up with an [SSL certificate](/docs/guides/security/ssl/). +1. Your application should now be running — visit it by navigating to your server's domain name in your browser. Make sure you prefix your domain name with `http` rather than `https`, as the server has not been set up with an [SSL certificate](/docs/guides/security/ssl/). ## Conclusion -You now have your own Laravel website up and running! To build on what you created by following this guide, be sure to take a look through [Laravel's documentation](https://laravel.com/docs/8.x). There, you can find plenty of information to dive deeper into the components of a Laravel application. +You now have your own Laravel website up and running! To build on what you created by following this guide, be sure to take a look through [Laravel's documentation](https://laravel.com/docs/8.x). There, you can find plenty of information to dive deeper into the components of a Laravel application. \ No newline at end of file