-
-
Notifications
You must be signed in to change notification settings - Fork 5
Lecture 1.3: Environment Setup, Github
In this part of the course, we will learn how to setup an Apache/PHP/MySql environment on your local machine. A common lingo that you might have heard is the word stack. A stack is typically the word used to denote an environment that is composed of an Operating System (Linux for example), a Web Server (Apache for example), a Database Server (MySql for example) and a Programming Language (PHP for example). So, on production servers that are usually operated by Linux, the stack will be a LAMP (Linux, Apache, MySql, PHP).
In this course, we will look at how to deploy a stack on a personal computer, either a Mac or a Windows, by trying to mirror the setup on a typical production server. We will also show in the videos how we could set up a production server on Digital Ocean but this is optional given that most hosting services are not free.
In order to deploy a stack on our personal computers, we could decide to download and configure Apache, MySql, PHP servers individually or we could use a prepackaged program that manages them all. There are 2 programs that are suitable for this class and they both work on any platform but we recommend using MAMP for Mac and XAMPP for Windows.
-
Download MAMP and install it. You can use the free version, the PRO version is not necessary for this course.
-
Once the installation is complete, open the program and go to
MAMP > Preferences > Web Server
from the menu. Identify where theDocument Root
is. This is where your application should go. You can change it if you want, for as long as you remember what it is. -
From the same Preferences screen, click on the
Ports
tab and click on the buttonSet Web and MySql servers to ports 80 and 3306
. This will set the ports for your servers to be the default. By doing so, if you go to your browser, you just need to typehttp://localhost
without specifying the port (this implies http://localhost:80). -
Restart your server from the main screen. You should see green indicators next to
Apache Server
andMySQL server
. TheCloud
indicator is for no use for us in this course.
For more information about the installation process consult the MAMP documentation
-
Download XAMPP. It is free.
-
In the beginning of the process, you need to choose only the following when prompted for service selection:
- Apache
- MySQL
- PHP
- phpMyAdmin (we will learn more about it later)
-
The installation folder is
C:\xampp
by default. You can modify it if you want but make sure to remember where you installed it because theDocument Root
will be<xampp folder>\htdocs
. -
You will be prompted to download some additional services after that (example: Bitnami). You do not need them for this course.
-
Once the installation is complete, open XAMPP and from the Control Panel, click on the
Start
buttons next toApache
andMySQL
. -
The labels
Apache
andMySQL
should be highlighted in green if they ran successfully. You will also see the ports 80 and 3306 assigned to them respectively.
For more information about the installation process consult the XAMPP documentation
-
Create a file
myfirstpage.php
in theDocument Root
folder. Refer to the installation process to validate where that folder is. -
In this file, add the following code:
<?php
echo 'This is my first page, and it works!';
?>
- Open a browser, type
http://localhost/myfirstpage.php
Some applications (such as Composer) use PHP from the command line, so we want to verify which distribution of PHP command line.
You can see info about which php executable command line is using by running the following commands:
$ which php
/Applications/MAMP/bin/php/php7.2.x/bin/php
$ php --version
PHP 7.2.x (cli) (built: Jul 17 2018 11:26:48) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
$ php --ini
Configuration File (php.ini) Path: /Applications/MAMP/bin/php/php7.2.x/conf
Loaded Configuration File: /Library/Application Support/appsolute/MAMP PRO/conf/php7.2.x.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
This info should reflect an installation of PHP found in /Applications/MAMP/bin/php/
and at version >= 7.2.
If the output you see reflects something different follow these instructions to update your PATH variable
Composer is a standard packager for applications written in php. It is usually used to help you easily manage the installation and upgrade of third-party open-source applications or libraries that you need to use in your own code. Caligrafy uses several open-source libraries and in order for it to function properly, it relies on composer to automatically download them for you.
-
Open the Command Line
-
From the Command Line, make sure you are in the
/usr/local/bin
folder. This is where you will need to install composer. You might need to create it if it doesn't exist in your system. In order to that, run the command `sudo mkdir -p /usr/local/bin. -
Once in the
/usr/local/bin
directory, download and install composer by running the following command:
$ curl -sS https://getcomposer.org/installer | sudo php
- This will install a file
composer.phar
. You need to rename the filecomposer
instead. In order to do this, run the following command:
sudo mv composer.phar composer
- Test it out by simply typing
composer
. You should see an output that shows the version of the installed composer.
-
Download Composer and install it.
-
During the installation, you will need to specify the location of the command line PHP. Make sure to specify the XAMPP php path. Typically this path is
C:\xampp\php\php.exe
. -
Once the installation completed, restart Cmder by closing the window and reopening it.
-
Type
composer
in the command line. The output should show you the version that of the installed composer.
In the previous lecture, we talked about git
. Github is the service that we will be using in this course to have a backup repository in the cloud. Github is an industry standard for open-source code. In this section, we will learn how to setup Github and how we can start interfacing with it from the Command Line to upload to Github and to download it when we need it.
Create a Github account. You only need the free version for this course.
In order to interface with Github, we need to setup a key so that Github recognizes the local machine that you will be transferring files from and to.
Mac
- From the Command Line, go to the
.ssh
directory
$ cd ~/.ssh
- if an
.ssh
directory does not exists, you will need to create it
$ mkdir ~/.ssh
$ cd ~/.ssh
(you might need to add sudo
before the commands if you don't have admin privileges on the machine)
- Run the following command to generate an SSH Key
$ ssh-keygen -t rsa -C "[email protected]"
- You will be prompted to enter values. Press
Enter
all the way through. We will be taking all the default values.
Windows
- From the Command Line, go to the
.ssh
directory
$ cd %home%\.ssh
- if an
.ssh
directory does not exists, you will need to create it
$ mkdir %home%\.ssh
$ cd %home%\.ssh
- Run the following command to generate an SSH Key
$ ssh-keygen -t rsa -C "[email protected]"
Now that an SSH key has been created that identifies your machine, you need to add the SSH key to Github. In order to that:
-
Log in to the account you created on github
-
From your profile menu go to Settings and then selec†
SSH and GPG Keys
or click here -
In the
SSH and GPG Keys
section, clickNew SSH Key
-
In the
Title
, enter any title that helps you identify the key. For example: _Personal_Key -
In the
Key
area, you need to enter the public SSH Key that you created in the previous step. In order to do that you need to do the following from the Command Line on your machine:
$ cd ~/.ssh
$ cat id_rsa.pub
The output is a long of string of characters that looks like ssh-rsa [......] [email protected]
is the public key. Copy it from the Command Terminal and paste it in the Key
area.
-
Click on 'Add SSH Key`. This adds the newly created to the list.
-
Let's test it. From the Command Line, type
$ ssh -T [email protected]
. You might be prompted with a warning. Typeyes
and hitEnter
. The output should look like this:
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
The should be yours and that is how you know that you start interfacing with Github.
Now that github is setup and that we can interface with it from the command line, let's create our first git repository. The best way to do this is to start by creating a repository on github and then clone it locally on your local machine.
-
From Github, click on the '+' in the top right menu and select
New Repository
-
Choose a name for your repository. Let's pick
my-first-app
for example. -
Set it to
public
. You can only create public repositories with the free version of github which is ok for this course. -
Check the
initialize this repository with README
. This will automatically add a README file to the folder. ClickCreate Repository
. -
This will take you to the
my-first-app
repository page. You now need to clone this repository locally. In order to do that, click on theClone or download
button and make sure that in the pop-up that opens up you seeClone with SSH
. If it is not the case, click on the linkUse SSH
. -
Copy the address that is provided by clicking in the field or on the button next to the address.
-
From the Command Line, go to the
Document Root
. If you don't know what theDocument Root
, refer back to the Environment Setup section above. Once in theDocument Root
type the following:
$ git clone [email protected]:yourGithubUsername/my-first-app.git
If all went well, the repository should be downloaded locally. In order to check, type ls
. There should be a folder named my-first-app
. You should be able to access the folder by typing cd my-first-app
.
In order to track changes the changes that you make on your local machine and upload them to Github, there are 3 commands that you need to be use constantly:
Git status will give you a status of the changes that were made on the local repository in your machine.
- Get a list of all the changes made locally on the repository
$ git status
The output should look like this when no changes have been made
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
- From either the Command Line or the Code Editor of your choice, make a change to the
README.md
file that is in this repository. You can type anything in it for now. Save it and go back to the Command Line and rungit status
again. The output should now be similar to this:
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
add
is the command that allows you to stage the changes. Staging means that you are informing the version control program that the changes that you made are important. This will prevent any attempts to override your code accidentally.
$ git add <name-of-file>
or if you want to add all the changes:
$ git add .
commit
is the command that allows you to notify the repository that is on Github and anywhere else where it got cloned, that you made a revision to the file. Usually every revision needs a comment associate with it in order to inform others of the changes that you have made.
$ git commit -m 'We modified the README file'
This will commit all the files that got added to the staging and will associate only one message for all of them.
The output should look similar to this
[master 1cb73b6] We modified the README file
1 file changed, 1 insertion(+), 1 deletion(-)
push
is the command that allows to upload all the changes to the repository in Github.
$ git push
or
$ git push --all
This will push all the committed changes and the output should look like this:
Counting objects: 3, done.
Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:your-username/my-first-app.git
e646ab3..1cb73b6 master -> master
Go to Github and if all went well, the README
would have the changes that you made.
You did it!