Laravel:Installasi: Difference between revisions
Tag: visualeditor |
Tag: wikieditor |
||
| Line 121: | Line 121: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
sudo composer create-project laravel/laravel laravelapp | sudo composer create-project laravel/laravel laravelapp | ||
</syntaxhighlight> | |||
Or, you may create new Laravel projects by globally installing the Laravel installer via Composer: | |||
<syntaxhighlight lang="bash"> | |||
composer global require laravel/installer | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="bash"> | |||
laravel new example-app | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 11:22, 13 January 2023
Let's first install a webserver to host the Laravel application. You can either use Apache or Nginx web server. Here I am using an Apache web server.
To install apache2, type:
<syntaxhighlight lang="bash"> sudo apt install apache2 </syntaxhighlight>
Once installed, Apache should be running. If it's not, for whatever reason, start it:
<syntaxhighlight lang="bash"> sudo systemctl start apache2 </syntaxhighlight>
Then enable it to start on boot time.
<syntaxhighlight lang="bash"> sudo systemctl enable apache2 </syntaxhighlight>
To verify the status of Apache, execute:
<syntaxhighlight lang="bash"> sudo systemctl status apache2 </syntaxhighlight>
Step 2: Install PHP and additional PHP extensions
Laravel 8 requires PHP 7.3 or above. Thankfully, PHP 7.4 is available in Ubuntu repositories. So, install PHP and the following PHP extensions.
<syntaxhighlight lang="bash"> sudo apt install php libapache2-mod-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql </syntaxhighlight>
When the installation is complete, verify the PHP version.
<syntaxhighlight lang="bash"> php -v </syntaxhighlight>
Step 3: Create Database for Laravel Application
Next up, we will create a database for the Laravel application.
But first, we need to install a database server. Laravel supported database systems are MariaDB, MySQL, SQLite, Postgres, or SQL Server.
We will go with the MariaDB database engine.
<syntaxhighlight lang="bash"> sudo apt install mariadb-server </syntaxhighlight>
Once the database server is installed, log into the MariaDB prompt:
<syntaxhighlight lang="bash"> sudo mysql -u root -p </syntaxhighlight>
Once logged in create the database, database user, and grant all privileges to the database user.
<syntaxhighlight lang="bash"> CREATE DATABASE laravel_db; </syntaxhighlight>
<syntaxhighlight lang="bash"> CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'secretpassword'; </syntaxhighlight>
<syntaxhighlight lang="bash"> GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost'; </syntaxhighlight>
<syntaxhighlight lang="bash"> FLUSH PRIVILEGES; </syntaxhighlight>
<syntaxhighlight lang="bash"> QUIT; </syntaxhighlight>
Step 4: Install Composer
Composer is a dependency package manager for PHP. It provides a framework for managing libraries and dependencies and required dependencies. To use Laravel, first install composer.
To download Composer, invoke the command shown.
<syntaxhighlight lang="bash"> curl -sS https://getcomposer.org/installer | php </syntaxhighlight>
This downloads the composer.phar file.
Next, move the composer file to the /usr/local/bin path.
<syntaxhighlight lang="bash"> sudo mv composer.phar /usr/local/bin/composer </syntaxhighlight>
Assign execute permission:
<syntaxhighlight lang="bash"> sudo chmod +x /usr/local/bin/composer </syntaxhighlight>
Verify the Composer version installed:
<syntaxhighlight lang="bash"> composer --version </syntaxhighlight>
Composer version 2.1.3 is installed.
Step 5: Install Laravel 8 on Ubuntu
With Composer installed, the next course of action is to install Laravel. Navigate to the webroot directory, type:
<syntaxhighlight lang="bash"> cd /var/www/html </syntaxhighlight>
Now, install Laravel using the composer command, type:
<syntaxhighlight lang="bash"> sudo composer create-project laravel/laravel laravelapp </syntaxhighlight>
Or, you may create new Laravel projects by globally installing the Laravel installer via Composer:
<syntaxhighlight lang="bash"> composer global require laravel/installer </syntaxhighlight>
<syntaxhighlight lang="bash"> laravel new example-app </syntaxhighlight>
The command creates a new directory called laravelapp and installs all the files and directories for Laravel. Change the ownership of the Laravel directory to the webserver user and also the permissions:
<syntaxhighlight lang="bash"> sudo chown -R www-data:www-data /var/www/html/laravelapp </syntaxhighlight>
<syntaxhighlight lang="bash"> sudo chmod -R 775 /var/www/html/laravelapp/storage </syntaxhighlight>
Feel free to replace laravelapp with a preferred directory name.
Once the installation is done navigate to the installation directory and check the Laravel version.
<syntaxhighlight lang="bash"> cd laravelapp </syntaxhighlight>
Cek version
<syntaxhighlight lang="bash"> php artisan </syntaxhighlight>
Step 6: Configure Apache to serve Laravel site
Lastly, we need to set up the Apache webserver to host the Laravel site. For that to happen, we need to create a virtual host file.
<syntaxhighlight lang="bash"> sudo vim /etc/apache2/sites-available/laravel.conf </syntaxhighlight>
Next, past the content shown and replace the example.com ServerName directive with the FQDN or public IP of the server ( Or private IP in case the server is on a LAN network ).
<syntaxhighlight lang="apacheconf" line="1"> <VirtualHost *:80>
ServerName example.com ServerAdmin admin@example.com DocumentRoot /var/www/html/laravelapp/public
<Directory /var/www/html/laravelapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> </syntaxhighlight>
Save the changes and exit the file. Next, enable the Laravel site and Apache rewrite module using these two commands.
<syntaxhighlight lang="bash"> sudo a2ensite laravel.conf </syntaxhighlight>
<syntaxhighlight lang="bash"> sudo a2enmod rewrite </syntaxhighlight>
To apply the changes, restart Apache.
<syntaxhighlight lang="bash"> sudo systemctl restart apache2 </syntaxhighlight>