From 7939805687e75f8a46cd6c43a80e9fb3db88da81 Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Mon, 7 Mar 2022 21:18:44 +0000 Subject: Add Installing Docker on Arch Linux --- .../index.html | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'www/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.html') diff --git a/www/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.html b/www/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.html index c8d0a7f..844ba66 100644 --- a/www/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.html +++ b/www/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.html @@ -24,22 +24,22 @@ $ git checkout release

Create the Homestead.yaml file by using th $ echo "<?php phpinfo();" > ~/projects/testsite/public/index.php

Setting up SSH

I like to use unique ssh keys for servers that I connect to and that includes any virtual machines running on my local machine. The ssh-keygen command generates a new key that I store in a directory separate from my other ones.

$ mkdir  ~/.ssh/homestead
 
-$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/homestead/id_rsa

Hostname Resolution

Since I am using one instance of Homestead for multiple sites I need to configure the host machine so that requests are directed to the correct site on the virtual machine. This is done by adding an entry into the /etc/hosts file for each site.

First I need to know the IP address of the virtual machine. This can be done by looking in the Homestead.yaml file for the ip entry.

ip: "192.168.10.10"

Then for each site that will be hosted on the virtual machine add it's domain and ip to the /etc/hosts file.

$ sudo nvim /etc/hosts
192.168.10.10 testsite.local

Configuring Homestead

Homestead is configured by editing the Homestead.yaml file that was created with the init.sh command earlier.

$ cd ~/.local/share/homestead
+$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/homestead/id_rsa

Hostname Resolution

Since I am using one instance of Homestead for multiple sites I need to configure the host machine so that requests are directed to the correct site on the virtual machine. This is done by adding an entry into the /etc/hosts file for each site.

First I need to know the IP address of the virtual machine. This can be done by looking in the Homestead.yaml file for the ip entry.

ip: "192.168.10.10"

Then for each site that will be hosted on the virtual machine add it's domain and ip to the /etc/hosts file.

$ sudo nvim /etc/hosts
192.168.10.10 testsite.local

Configuring Homestead

Homestead is configured by editing the Homestead.yaml file that was created with the init.sh command earlier.

$ cd ~/.local/share/homestead
 
 $ nvim Homestead.yaml

First tell Vagrant that Virtualbox will be providing the virtual machine.

set provider: virtualbox

Vagrant needs to setup the ssh keys between the host and the guest so that you can connect via ssh. Enter the path to the one created earlier.

authorize: ~/.ssh/homestead/id_rsa.pub
 
 keys:
     - ~/.ssh/homestead/id_rsa

Share the project folder with the virtual machine. This setting will make the directory /home/vagrant/projects/testsite available in the virtual machine. The contents of this directory will be shared with the host machine directory ~/projects/testsite.

folders:
     - map: ~/projects/testsite
-      to: /home/vagrant/projects/testsite

Setup Homestead so that it can serve the application through the 'domain' testsite.local. Note how this matches the name added to /etc/hosts earlier.

sites:
+      to: /home/vagrant/projects/testsite

Setup Homestead so that it can serve the application through the 'domain' testsite.local. Note how this matches the name added to /etc/hosts earlier.

sites:
     - map: testsite.local
       to: /home/vagrant/projects/testsite/public

Have Homestead create a database for our application.

databases:
-    - testsite

Since I'm using a database ensure that a database server is installed on the virtual machine.

features:
+    - testsite

Since I'm using a database ensure that a database server is installed on the virtual machine.

features:
     - mariadb: true

Launching Homestead

Homestead is started with the vagrant up command. It may take a while for Homestead to launch if this is the first time running this command as Vagrant has to first download the actual virtual machine file.

$ cd ~/.local/share/homestead
 
 $ vagrant up

Once the machine is booted I can browse to http://testsite.local/ to see the simple site that is now served by Homestead.

Installing a Laravel Site

Now that Homestead is installed and serving a simple site its time to move onto installing the first Laravel application. Since Homestead provides all the tools required to do this the first thing to do is connect to the virtual machine.

$ cd ~/.local/share/homestead
 
-$ vagrant ssh

Once connected to the virtual machine navigate to the project folder of the site. Remember that this is the folder that is also been shared with the host machine.

$ cd ~/projects/testsite

Clear the contents of this folder otherwise composer will complain about a non-empty directory.

$ rm -rf public

Use composer to install a Laravel project.

$ composer create-project laravel/laravel .

Setting Up The Application Database

Once Larvel is installed a database needs to be created for the application. Connect to the database server with the mysql command.

$ mysql -uhomestead -psecret

Check that the application's database was created when the virtual machine was first booted.

SHOW DATABASES;
+$ vagrant ssh

Once connected to the virtual machine navigate to the project folder of the site. Remember that this is the folder that is also been shared with the host machine.

$ cd ~/projects/testsite

Clear the contents of this folder otherwise composer will complain about a non-empty directory.

$ rm -rf public

Use composer to install a Laravel project.

$ composer create-project laravel/laravel .

Setting Up The Application Database

Once Larvel is installed a database needs to be created for the application. Connect to the database server with the mysql command.

$ mysql -uhomestead -psecret

Check that the application's database was created when the virtual machine was first booted.

SHOW DATABASES;
 
 +--------------------+
 | Database           |
@@ -50,16 +50,16 @@ $ vagrant ssh

Once connected to the virtual machine navigate to t | performance_schema | | sys | | testsite | -+--------------------+

If the database does not exist create it with the below SQL.

CREATE DATABASE testsite;

Create a MySQL user for the application and grant permissions to use the application database.

CREATE USER 'testsite'@'localhost' IDENTIFIED BY 'testsite';
++--------------------+

If the database does not exist create it with the below SQL.

CREATE DATABASE testsite;

Create a MySQL user for the application and grant permissions to use the application database.

CREATE USER 'testsite'@'localhost' IDENTIFIED BY 'testsite';
 
-GRANT ALL PRIVILEGES ON testsite.* TO 'testsite'@'localhost';

Exit the database server.

exit

To check the new MySql user account simply connect as that user with the credentials used earlier.

$ mysql -utestsite -ptestsite

The result of the SHOW DATABASE sql should show that the user account can see the application database.

SHOW DATABASES;
+GRANT ALL PRIVILEGES ON testsite.* TO 'testsite'@'localhost';

Exit the database server.

exit

To check the new MySql user account simply connect as that user with the credentials used earlier.

$ mysql -utestsite -ptestsite

The result of the SHOW DATABASE sql should show that the user account can see the application database.

SHOW DATABASES;
 
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | testsite           |
-+--------------------+

Configuring The Laravel Application

Edit the supplied .env file.

$ vim .env

If one does not exist then copy the example file.

$ cp .env.example .env

Change the environment variables to match those below. Some of changes ensure that Laravel can connect to the database created earlier.

APP_NAME='Test Site'
++--------------------+

Configuring The Laravel Application

Edit the supplied .env file.

$ vim .env

If one does not exist then copy the example file.

$ cp .env.example .env

Change the environment variables to match those below. Some of changes ensure that Laravel can connect to the database created earlier.

APP_NAME='Test Site'
 
 APP_URL=http://testsite.local
 
@@ -68,9 +68,9 @@ DB_HOST=127.0.0.1
 DB_PORT=3306
 DB_DATABASE=testsite
 DB_USERNAME=testsite
-DB_PASSWORD=testsite

Now when you browse to http://testsite.local you will see the Laravel welcome page.

Simplified SSH

I prefer to just use the host system's ssh command to connect to Homestead as it cuts out having to navigate to the Homestead directory and running vagrant ssh.

To simplify ssh I first add a hostname for the virtual machine to the file /etc/hosts/

192.168.10.10 homestead

I then edit ~/.ssh/config and add the below configuration. This tells ssh to automatically use the keys and username specified when connecting to the virtual machine.

Host homestead
+DB_PASSWORD=testsite

Now when you browse to http://testsite.local you will see the Laravel welcome page.

Simplified SSH

I prefer to just use the host system's ssh command to connect to Homestead as it cuts out having to navigate to the Homestead directory and running vagrant ssh.

To simplify ssh I first add a hostname for the virtual machine to the file /etc/hosts/

192.168.10.10 homestead

I then edit ~/.ssh/config and add the below configuration. This tells ssh to automatically use the keys and username specified when connecting to the virtual machine.

Host homestead
   IdentityFile ~/.ssh/homestead/id_rsa
-  User vagrant

From now on I can simply do ssh homestead from any directory to connect to the Homestead virtual machine.

Links

Laravel HomesteadLaravel - Read More Posts.

I don't have comments as I don't want to manage them. You can however contact me at the below address if you want to.

Email david@davidtsadler.com

License

The contents of this site is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Copyright © 2021 David T. Sadler.

Return to Homepage. + User vagrant

From now on I can simply do ssh homestead from any directory to connect to the Homestead virtual machine.

Links

Laravel HomesteadLaravel - Read More Posts.

I don't have comments as I don't want to manage them. You can however contact me at the below address if you want to.

Email david@davidtsadler.com

License

The contents of this site is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Copyright © 2021 David T. Sadler.

Return to Homepage. -- cgit v1.2.3-13-gbd6f