Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Last active April 29, 2018 12:57
Show Gist options
  • Select an option

  • Save gemmadlou/d84f0ac440351f6c8561ead1c6d02290 to your computer and use it in GitHub Desktop.

Select an option

Save gemmadlou/d84f0ac440351f6c8561ead1c6d02290 to your computer and use it in GitHub Desktop.
Creating a new PHP Nginx Server Ubuntu 16.04

Notes On Setting Up PHP Nginx with Mongo

Create a new droplet: https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets.

Syncing vs code with droplet: https://github.com/liximomo/vscode-sftp

> .vscode/sftp.json
{
   "context": ".",
   "protocol": "sftp",
   "host": "111.111.111.111",
   "port": 22,
   "username": "root",
   "remotePath": "/var/www/vhosts/wordpress/",
   "connectTimeout": 10000,
   "uploadOnSave": false,
   "downloadOnOpen": false,
   "syncMode": "update",
   "ignore": [],
   "privateKeyPath": "/home/gemma/.ssh/somekey",
   "watcher": {
       "files": "**/*",
       "autoUpload": true,
       "autoDelete": true
   }
}

Setting up docker: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04

Install Nginx: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04

PHP and Nginx https://linuxconfig.org/basic-php-7-and-nginx-configuration-on-ubuntu-16-04-linux

Mongo https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Mongo - PHP Driver: http://php.net/manual/en/mongodb.installation.pecl.php

Ubuntu 16.04

sudo apt-get install libcurl4-openssl-dev pkg-config libssl-dev libsslcommon2-dev

sudo pecl install mongodb

add extension=mongodb.so in fpm and cli:

sudo vim /etc/php/7.0/fpm/conf.d/30-mongodb.ini

sudo vim /etc/php/7.0/cli/conf.d/30-mongodb.ini

Restart service:

sudo systemctl restart php7.0-fpm

sudo systemctl reload nginx

Configuring Droplets From Command Line

Instead of going through a GUI, we can speed up the configuration and spinning up of droplets using the command line. This means we can even automate instance creation. Ideally, we could have a node js command line script that generates droplets, then use another tool to configure the server and set everything up for us.

Therefore, specialist knowledge needn't be required to setup a new server, we could merely pass a script on to front end developers or QA personnel, and they set up a project withoutany Devops or technical know-how.

More importantly, it would save us time. This section goes through the following:

  • Getting started with the Digital Ocean API
  • Create a new droplet
  • List droplets
  • Create multiple droplets
  • Delete a droplet
  • List droplets by tagname
  • Deleting a droplet by tagname

Requirements

  • Terminal
  • Digital ocean account
  • Python
  • CURL

Getting Started With the Digital Ocean API

Generate token

  • Go the (1) API page and then click (2) generate token

  • Add a token name

Important: record this token. It will not show up again.

You can also save the token in bash like so:

$ export DIGITAL_OCEAN_TOKEN=abcadadasdadadadsdaddasdadadasdadadasdasdasdada

Testing the token works

! bash
curl -X GET "https://api.digitalocean.com/v2/actions" \ 
	-H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
	| python -m json.tool

Python pipe is to pretty print the returning json - so it's easier to read

List all droplets on account

! bash
curl -X GET "https://api.digitalocean.com/v2/droplets" \
	-H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
	| python -m json.tool

Create a droplet

! bash
curl -X POST "https://api.digitalocean.com/v2/droplets" \
	-d'{"name":"My-Droplet","region":"nyc3","size":"512mb","image":"ubuntu-14-04-x64"}' \
	-H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
	-H "Content-Type: application/json" \
	| python -m json.tool

Create multiple droplets

! bash
curl -X POST "https://api.digitalocean.com/v2/droplets" \
	-d'{"names":["sub-01.example.com","sub-02.example.com"],"region":"nyc3","size":"512mb","image":"ubuntu-16-04-x64"}' \
	-H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
	-H "Content-Type: application/json" \
	| python -m json.tool

Delete a droplet

WORK ON THIS

! bash
curl -H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
	-H "Content-Type: application/json" \
	-X DELETE "https://api.digitalocean.com/v2/droplets/88226674"

Get a droplet by tagname

@todo

Delete a droplet by tagname

@todo

Resources

https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-api-v2

Docker

List Docker CLI commands

docker docker container --help

Display Docker version and info

docker --version docker version docker info

Execute Docker image

docker run hello-world

In detached mode

docker run -d -p 4000:80 imagename

Stop container

docker container stop CONTAINER_ID

List Docker images

docker image ls

List Docker containers (running, all, all in quiet mode)

docker container ls docker container ls --all docker container ls -aq

Remove specified image from this machine

docker image rm


Login

docker login

Tag image

docker tag IMAGE_NAME USERNAME/IMAGE_PACKAGE_NAME:VERSION_TAG_OR_NAME

Publishing an image

docker push USERNAME/IMAGE_PACKAGE_NAME:VERSION_TAG_OR_NAME

Docker run your public repository

docker run -p 4000:80 username/repository:tag


Persisting Data with volumes

Adding a volume

docker volume create

Removing a volume

docker volume prune

Start a container with a volume

docker run -d \
  --name devtest \
  --mount source=myvol2,target=/app \
  nginx:latest

List volumes

docker volume ls

Inspect volumes

docker volume inspect my-vol

Remove volume

docker volume rm my-vol


Starting a docker container and running commands interactively

docker run -it DOCKER_IMAGE sh

WARNING - remove all exited docker containers

docker rm $(docker ps -a -q -f status=exited)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment