summaryrefslogtreecommitdiff
path: root/src/posts/laravel
diff options
context:
space:
mode:
Diffstat (limited to 'src/posts/laravel')
-rw-r--r--src/posts/laravel/2020-12-14/sqlstate-hy000-2002-php-network-getaddresses-getaddrinfo-failed/index.gmi55
-rw-r--r--src/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.gmi315
-rw-r--r--src/posts/laravel/index.gmi10
3 files changed, 380 insertions, 0 deletions
diff --git a/src/posts/laravel/2020-12-14/sqlstate-hy000-2002-php-network-getaddresses-getaddrinfo-failed/index.gmi b/src/posts/laravel/2020-12-14/sqlstate-hy000-2002-php-network-getaddresses-getaddrinfo-failed/index.gmi
new file mode 100644
index 0000000..76e4c91
--- /dev/null
+++ b/src/posts/laravel/2020-12-14/sqlstate-hy000-2002-php-network-getaddresses-getaddrinfo-failed/index.gmi
@@ -0,0 +1,55 @@
+# SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed
+
+> Mon 14th December 2020 By David T. Sadler.
+
+One of the first things you do when creating a new Larvel application is run php artisan migrate to create the application database tables. However you may come across the below message.
+
+```shell
+$ php artisan migrate
+
+Illuminate\Database\QueryException
+
+ SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from information_schema.tables where table_schema = testsite and table_name = migrations and table_type = 'BASE TABLE')
+
+ at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
+ 674▕ // If an exception occurs when attempting to run a query, we'll format the error
+ 675▕ // message to include the bindings with SQL, which will make this exception a
+ 676▕ // lot more helpful to the developer instead of just the database's errors.
+ 677▕ catch (Exception $e) {
+ 678▕ throw new QueryException(
+ 679▕ $query, $this->prepareBindings($bindings), $e
+ 680▕ );
+ 681▕ }
+ 682▕
+
+ +33 vendor frames
+ 34 artisan:37
+ Illuminate\Foundation\Console\Kernel::handle()
+```
+
+The cause of this issue is due to a change introduced to the .env.example file. This changed the environment variable DB_HOST from 127.0.0.1 to mysql. The reason for this change is to support [Laravel Sail](https://laravel.com/docs/8.x/sail) which is a Docker development environment for Laravel.
+
+The change means your Laravel application will try and connect to a database server with the hostname of mysql. Unless this exists then the application can't connect.
+
+To resolve the issue just change the value back to 127.0.0.1
+
+```shell
+DB_HOST=127.0.0.1
+```
+
+### Links
+
+=> https://github.com/laravel/laravel/commit/a895748980b3e055ffcb68b6bc1c2e5fad6ecb08#diff-a3046da0d15a27e89f2afe639b25748a7ad4d9290af3e7b1b6c1a5533c8f0a8cL11 Commit that changed .env.example.
+=> https://laravel.com/docs/8.x/sail/ Laravel Sail
+
+=> /posts/larvel/ Laravel - 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.
+
+=> mailto:david@davidtsadler.com Email david@davidtsadler.com
+
+=> https://creativecommons.org/licenses/by-sa/4.0/ 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.
diff --git a/src/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.gmi b/src/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.gmi
new file mode 100644
index 0000000..5fd1807
--- /dev/null
+++ b/src/posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/index.gmi
@@ -0,0 +1,315 @@
+# Installing Laravel Homestead in Arch Linux
+
+> Mon 21st December 2020 By David T. Sadler.
+
+## Introduction
+
+Laravel Homestead is a pre-packaged Vagrant box that gives you a development environment with PHP, a web server, and several other server software already installed. This guide explains how I install and configure it on my Arch Linux system. I install Homestead in such a way that enables me to add multiple projects to the single instance of Homestead. Because of this you may find that you will need to adapt the guide to how you prefer to manage your projects.
+
+## Installing Dependencies
+
+Vagrant will need to be installed since Laravel Homestead is a pre-configured Vagrant box. Select virtualbox if you are prompted for which provider Vagrant should use.
+
+```shell
+$ sudo pacman -S vagrant
+```
+
+Install Virtualbox as this will be used as a provider by Vagrant. When prompted select the virtualbox-host-modules-arch package.
+
+```shell
+$ sudo pacman -S virtualbox
+```
+
+## Install Homestead
+
+Homestead is installed by cloning the repository onto your host machine.
+
+```shell
+$ git clone https://github.com/laravel/homestead.git ~/.local/share/homestead
+```
+
+The cloned repository defaults to the latest master branch. As this can be considered unstable it is recommended to checkout a tagged version of Homestead or the release branch as this will always be the latest stable version.
+
+```shell
+$ cd ~/.local/share/homestead
+
+$ git checkout release
+```
+
+Create the Homestead.yaml file by using the init.sh command.
+
+```shell
+$ bash init.sh
+```
+
+## Configuring the host machine
+
+Before I launch Homestead for the first time there are a few things that I like to do first on the host machine.
+
+### Creating a host project
+
+One of the advantages with Homestead is that it allows you to share a directory on your host machine with the virtual guest machine and have that served by the web server.
+
+For this guide the commands below will create a very simple PHP site.
+
+```shell
+$ mkdir -p ~/projects/testsite/public
+
+$ 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.
+
+```shell
+$ 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.
+
+```yaml
+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.
+
+```shell
+$ sudo nvim /etc/hosts
+```
+
+```shell
+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.
+
+```shell
+$ cd ~/.local/share/homestead
+
+$ nvim Homestead.yaml
+```
+
+First tell Vagrant that Virtualbox will be providing the virtual machine.
+
+```yaml
+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.
+
+```yaml
+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.
+
+```yaml
+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.
+
+```yaml
+sites:
+ - map: testsite.local
+ to: /home/vagrant/projects/testsite/public
+```
+
+Have Homestead create a database for our application.
+
+```yaml
+databases:
+ - testsite
+```
+
+Since I'm using a database ensure that a database server is installed on the virtual machine.
+
+```yaml
+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.
+
+```shell
+$ 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.
+
+```shell
+$ 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.
+
+```shell
+$ cd ~/projects/testsite
+```
+
+Clear the contents of this folder otherwise composer will complain about a non-empty directory.
+
+```shell
+$ rm -rf public
+```
+
+Use composer to install a Laravel project.
+
+```shell
+$ 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.
+
+```shell
+$ mysql -uhomestead -psecret
+```
+
+Check that the application's database was created when the virtual machine was first booted.
+
+```mysql
+SHOW DATABASES;
+
++--------------------+
+| Database |
++--------------------+
+| homestead |
+| information_schema |
+| mysql |
+| performance_schema |
+| sys |
+| testsite |
++--------------------+
+```
+
+If the database does not exist create it with the below SQL.
+
+```mysql
+CREATE DATABASE testsite;
+```
+
+Create a MySQL user for the application and grant permissions to use the application database.
+
+```mysql
+CREATE USER 'testsite'@'localhost' IDENTIFIED BY 'testsite';
+
+GRANT ALL PRIVILEGES ON testsite.* TO 'testsite'@'localhost';
+```
+
+Exit the database server.
+
+```mysql
+exit
+```
+
+To check the new MySql user account simply connect as that user with the credentials used earlier.
+
+```shell
+$ mysql -utestsite -ptestsite
+```
+
+The result of the SHOW DATABASE sql should show that the user account can see the application database.
+
+```mysql
+SHOW DATABASES;
+
++--------------------+
+| Database |
++--------------------+
+| information_schema |
+| testsite |
++--------------------+
+```
+
+## Configuring The Laravel Application
+
+Edit the supplied .env file.
+
+```shell
+$ vim .env
+```
+
+If one does not exist then copy the example file.
+
+```shell
+$ 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
+
+DB_CONNECTION=mysql
+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/
+
+```shell
+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.
+
+```ssh
+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
+
+=> https://laravel.com/docs/8.x/homestead/ Laravel Homestead
+
+=> /posts/larvel/ Laravel - 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.
+
+=> mailto:david@davidtsadler.com Email david@davidtsadler.com
+
+=> https://creativecommons.org/licenses/by-sa/4.0/ 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.
diff --git a/src/posts/laravel/index.gmi b/src/posts/laravel/index.gmi
new file mode 100644
index 0000000..6227b2c
--- /dev/null
+++ b/src/posts/laravel/index.gmi
@@ -0,0 +1,10 @@
+# All Posts Tagged Laravel
+
+=> /posts/laravel/2020-12-21/installing-laravel-homestead-in-arch-linux/ 2020-12-21 - Installing Laravel Homestead in Arch Linux
+=> /posts/laravel/2020-12-14/sqlstate-hy000-2002-php-network-getaddresses-getaddrinfo-failed/ 2020-12-14 - SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed
+
+=> https://creativecommons.org/licenses/by-sa/4.0/ 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.