Bu örneğimizde vagrant ile tam olarak çalışan bir LAMP ortamı yaratacağız ve bunu symfony uygulaması geliştirmek için kullanacağız. Apache sunucusu ve phpMyAdmin ana bilgisayarın işletim sisteminden ulaşılabilir halde olacak. Bu şekilde symfony uygulamamıza ana bilgisayardan da ulaşabileceğiz. Bunlarla birlikte kutu içinde php-cs-fixer, phpspec, behat vs. gibi testleride çalıştırabileceğiz.

Projenin ismi travis; kutudaki domain adresi travis.dev; uygulama ana bilgisayarın işletim sisteminden http://192.168.50.10/ ile ulaşılacak; phpMyAdmin GUI http://192.168.50.10/phpmyadmin ile ulaşılacak ve login bilgileri root:root olacak; projenin dosyaları ana işletim sistemindeki inanzzz:mac-os $ ..../ubuntu-trusty-lamp/public klasöründe ve kutudaki vagrant@lamp:/$ ls -l /var/www/html/ klasöründe olacak; paylaşılan klasör public ve var/www/html her iki ortam içinde de yazılabilir olacaklar.


Konfigürasyon


Sisteminizde vagrant ve Oracle VM yazılımlarının kurulu olduğunu varsayıyorum. Bununla birlikte ubuntu/trusty64 kutusunun sisteminize vagrant box add ubuntu/trusty64 komutu ile eklendiğini de varsayıyorum. Eğer emin değilseniz ls -l ~/.vagrant.d/boxes/ komutu ile kontrol edebilirsiniz.


Yeni proje klasörü yaratmak


$ mkdir ubuntu-trusty-lamp
$ cd ubuntu-trusty-lamp/

bootstrap.sh dosyası


Bu işlemi ubuntu-trusty-lamp klasöründeyken yapacaksınız. Dosyayı bootstrap.sh olarak adlandırın.


#!/usr/bin/env bash

# BEGIN ########################################################################
echo -e "-- ------------------ --\n"
echo -e "-- BEGIN BOOTSTRAPING --\n"
echo -e "-- ------------------ --\n"

# VARIABLES ####################################################################
echo -e "-- Setting global variables\n"
APACHE_CONFIG=/etc/apache2/apache2.conf
PHP_INI=/etc/php5/apache2/php.ini
SITES_ENABLED=/etc/apache2/sites-enabled
PHPMYADMIN_CONFIG=/etc/phpmyadmin/config-db.php
DOCUMENT_ROOT=/var/www/html/travis/web
VIRTUALHOST=travis.dev
LOCALHOST=localhost
MYSQL_DATABASE=symfony
MYSQL_USER=root
MYSQL_PASSWORD=root

# BOX ##########################################################################
echo -e "-- Updating packages list\n"
apt-get update -y -qq

# APACHE #######################################################################
echo -e "-- Installing Apache web server\n"
apt-get install -y apache2 > /dev/null 2>&1

echo -e "-- Adding ServerName to Apache config\n"
grep -q "ServerName ${LOCALHOST}" "${APACHE_CONFIG}" || echo "ServerName ${LOCALHOST}" >> "${APACHE_CONFIG}"

echo -e "-- Allowing Apache override to all\n"
sed -i "s/AllowOverride None/AllowOverride All/g" ${APACHE_CONFIG}

echo -e "-- Updating vhost file\n"
cat > ${SITES_ENABLED}/000-default.conf <<EOF
<VirtualHost *:80>
ServerName ${VIRTUALHOST}
DocumentRoot ${DOCUMENT_ROOT}

<Directory ${DOCUMENT_ROOT}>
Options Indexes FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/${VIRTUALHOST}-error.log
CustomLog ${APACHE_LOG_DIR}/${VIRTUALHOST}-access.log combined
</VirtualHost>
EOF

echo -e "-- Creating the DocumentRoot directory structure\n"
mkdir -p ${DOCUMENT_ROOT}

# PHP ##########################################################################
echo -e "-- Fetching PHP 5.6 repository\n"
add-apt-repository -y ppa:ondrej/php5-5.6 > /dev/null 2>&1

echo -e "-- Updating packages list\n"
apt-get update -y -qq

echo -e "-- Installing PHP modules\n"
apt-get install -y python-software-properties > /dev/null 2>&1
apt-get install -y libapache2-mod-php5 > /dev/null 2>&1
apt-get install -y php5 > /dev/null 2>&1
apt-get install -y php5-cli > /dev/null 2>&1
apt-get install -y php5-mcrypt > /dev/null 2>&1
apt-get install -y php5-curl > /dev/null 2>&1

echo -e "-- Enabling PHP mcrypt module\n"
php5enmod mcrypt

echo -e "-- Turning PHP error reporting on\n"
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" ${PHP_INI}
sed -i "s/display_errors = .*/display_errors = On/" ${PHP_INI}

# MYSQL ########################################################################
echo -e "-- Installing MySQL server\n"
debconf-set-selections <<< "mysql-server mysql-server/root_password password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ${MYSQL_PASSWORD}"

echo -e "-- Installing MySQL packages\n"
apt-get install -y mysql-server > /dev/null 2>&1
apt-get install -y libapache2-mod-auth-mysql > /dev/null 2>&1
apt-get install -y php5-mysql > /dev/null 2>&1

echo -e "-- Setting up a dummy MySQL database\n"
mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -h ${LOCALHOST} -e "CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE}"

# PHPMYADMIN ###################################################################
echo -e "-- Installing phpMyAdmin GUI\n"
debconf-set-selections <<< "phpmyadmin phpmyadmin/dbconfig-install boolean true"
debconf-set-selections <<< "phpmyadmin phpmyadmin/app-password-confirm password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/admin-pass password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/app-pass password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2"

echo -e "-- Installing phpMyAdmin package\n"
apt-get install -y phpmyadmin > /dev/null 2>&1

echo -e "-- Setting up phpMyAdmin GUI login user\n"
sed -i "s/dbuser='phpmyadmin'/dbuser='${MYSQL_USER}'/g" ${PHPMYADMIN_CONFIG}

# SQLITE #######################################################################
echo -e "-- Installing SQLite and relative PHP module\n"
apt-get install -y php5-sqlite > /dev/null 2>&1
apt-get install -y sqlite > /dev/null 2>&1
apt-get install -y sqlite3 > /dev/null 2>&1
apt-get install -y libsqlite3-dev > /dev/null 2>&1

# COMPOSER #####################################################################
echo -e "-- Installing PHP cURL module\n"
apt-get install -y curl > /dev/null 2>&1

echo -e "-- Setting up Composer\n"
curl -sSk https://getcomposer.org/installer | php -- --disable-tls > /dev/null 2>&1
mv composer.phar /usr/local/bin/composer

# GIT ##########################################################################
echo -e "-- Installing Git\n"
apt-get install -y git > /dev/null 2>&1

# GITHUB #######################################################################
echo -e "-- Enabling SSH connection to GitHub\n"
mkdir -p ~/.ssh > /dev/null 2>&1
ssh-keyscan -H github.com >> ~/.ssh/known_hosts > /dev/null 2>&1

# REFRESH ######################################################################
echo -e "-- Restarting Apache web server\n"
service apache2 restart

# END ##########################################################################
echo -e "-- ---------------- --"
echo -e "-- END BOOTSTRAPING --"
echo -e "-- ---------------- --"

Vagrantfile içeriği


Bu işlemi ubuntu-trusty-lamp klasöründeyken yapacaksınız. Dosyayı Vagrantfile olarak adlandırın.


# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
# Type of the box
config.vm.box = "ubuntu/trusty64"

# Used for SSH and GitHub connections
config.ssh.forward_agent = true

# Configs fo virtual machine
config.vm.define :lamp do |lamp_config|
# Terminal name
lamp_config.vm.hostname = "lamp"
# IP address to access from host machine
lamp_config.vm.network "private_network", ip: "192.168.50.10"
# Script to run while setting up the machine
lamp_config.vm.provision :shell, path: "bootstrap.sh"
end

# Configs for virtual machine entry in Oracle VM VirtualBox
config.vm.provider :virtualbox do |vb_config|
vb_config.name = "Vagrant - Ubuntu 14.04 LAMP"
end

# Shared folder between host and virtual machine with "faster" nfs option and writing permissions enabled
# NFS option speeds up composer processes
# Mouth options grants writing permissions to '/var/www/html' folder
# Mount option below will assign "501 dialout" owner:group permissions to shared folder
# so that you can do whatever you want with it within host and guest machines.
config.vm.synced_folder "public", "/var/www/html", create: true, nfs: true, mount_options: ["actimeo=2"]
end

Vagrant kutusunu başlatmak


Eğer "bootstrap.sh" dosyasının içeriğinde değişiklik yaparsanız, vagrant reload --provision komutunu kullanmanız gerekecek. Bu işlem sırasında, admin şifresi sorulacak ve oraya işletim sisteminizin şifresini gireceksiniz. Eğer şifre girmeyi iptal etmek istiyorsanız nfs adresini okuyabilirsiniz.



==> lamp: ......
==> lamp: ......
==> lamp: ......
==> lamp: -- ------------------ --
==> lamp: -- BEGIN BOOTSTRAPING --
==> lamp: -- ------------------ --
==> lamp: -- Setting global variables
==> lamp: -- Updating packages list
==> lamp: -- Installing Apache web server
==> lamp: -- Adding ServerName to Apache config
==> lamp: -- Allowing Apache override to all
==> lamp: -- Updating vhost file
==> lamp: -- Creating the DocumentRoot directory structure
==> lamp: -- Fetching PHP 5.6 repository
==> lamp: -- Updating packages list
==> lamp: -- Installing PHP modules
==> lamp: -- Enabling PHP mcrypt module
==> lamp: -- Turning PHP error reporting on
==> lamp: -- Installing MySQL server
==> lamp: -- Installing MySQL packages
==> lamp: -- Setting up a dummy MySQL database
==> lamp: -- Installing phpMyAdmin GUI
==> lamp: -- Installing phpMyAdmin package
==> lamp: -- Setting up phpMyAdmin GUI login user
==> lamp: -- Installing SQLite and relative PHP module
==> lamp: -- Installing PHP cURL module
==> lamp: -- Setting up Composer
==> lamp: -- Installing Git
==> lamp: -- Enabling SSH connection to GitHub
==> lamp: -- Restarting Apache web server
==> lamp: * Restarting web server apache2
==> lamp: ...done.
==> lamp: -- ---------------- --
==> lamp: -- END BOOTSTRAPING --
==> lamp: -- ---------------- --

Kutuya girmek


$ vagrant ssh
vagrant@lamp:~$

Yüklenen servislerin kontrolü


vagrant@lamp:~$ uname -mrs
Linux 3.13.0-88-generic x86_64

vagrant@lamp:~$ apache2 -v
Server version: Apache/2.4.7 (Ubuntu)
Server built: May 4 2016 17:05:10

vagrant@lamp:~$ sqlite -version
2.8.17

vagrant@lamp:~$ php -v
PHP 5.6.23-1+deprecated+dontuse+deb.sury.org~trusty+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

vagrant@lamp:~$ composer --version
Composer version 1.1.3 2016-06-26 15:42:08

vagrant@lamp:~$ git --version
git version 1.9.1

mysql> SELECT VERSION();
+-------------------------+
| VERSION() |
+-------------------------+
| 5.5.49-0ubuntu0.14.04.1 |
+-------------------------+
1 row in set (0.00 sec)

Git konfigürasyon


Hesap ile ilgili bilgileri aşağıdaki gibi girin.


vagrant@lamp:~$ git config --global user.name "You"
vagrant@lamp:~$ git config --global user.email "hello@world.com"
vagrant@lamp:~$ git config --list
user.name=You
user.email=hello@world.com

SSH kurulumu ve GitHub bağlantısı


SSH anahtarı sisteminizde henüz ayarlanmış değil ve buna bağlı olarak GitHub aktivasyonu yapılmamış durumda. Bu durumda aşağıdaki hatayı alacaksınız.


vagrant@lamp:~$ ssh -T git@github.com
Permission denied (publickey).

Bu adımda kendinizi GitHub'ı kullanmak için tanımlayacaksınız. Bunun için Set up Git adresindeki adımları uygulamanız gerekecek. Sorulan Enter passphrase (empty for no passphrase): sorusu için şifre girmeyin ve boş bırakın. Başarıyla tamamlanan işlemlerden sonra aşağıdaki mesajı alacaksınız.


vagrant@lamp:~$ ssh -T git@github.com
Hi You! You've successfully authenticated, but GitHub does not provide shell access.

GitHub projesini kopyalama


Vagrant provisioning /var/www/html/travis/web klasörünü apache hatası vermesin diye daha önceden yarattı. Öncelikle travis klasörünü vagrant@lamp:~$ sudo rm -Rf /var/www/html/travis komutu ile silin.


vagrant@lamp:/var/www/html$ git clone git@github.com:You/travis.git
Cloning into 'travis'...
remote: Counting objects: 603, done.
remote: Total 603 (delta 0), reused 0 (delta 0), pack-reused 603
Receiving objects: 100% (603/603), 126.36 KiB | 23.00 KiB/s, done.
Resolving deltas: 100% (316/316), done.
Checking connectivity... done.

Uygulamayı yükleme


vagrant@lamp:/var/www/html$ cd travis/
vagrant@lamp:/var/www/html/travis$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
- Installing doctrine/lexer (v1.0.1)
Loading from cache
......
......
......

Testleri çalıştırma


vagrant@lamp:/var/www/html/travis$ bin/phing
Buildfile: /var/www/html/travis/build.xml

travis > cache.clear:
[echo] Clearing application cache and logs ...
travis > cache.warm:
[echo] Warming up cache ...
travis > composer.update:
[echo] Running composer self-update ...
travis > composer.install:
[echo] Running composer install ...
travis > database.create:
[echo] Creating database ...
travis > doctrine.update.entities:
[echo] Updating doctrine entities ...
travis > doctrine.data.fixtures:
[echo] Loading doctrine data fixtures ...
travis > test.phpcsfixer:
[echo] Checking coding standards ...
travis > test.phpspec:
[echo] Running phpspec tests ...
travis > test.behat:
[echo] Running behat tests ...

BUILD FINISHED

Total time: 24.6246 seconds

Ana işletim sisteminden uygulamaya ulaşım testi


Sadece http://192.168.50.10/, http://192.168.50.10/app_dev.php veya http://192.168.50.10/app_test.php adreslerine gitmeyi deneyin. Ek olarak "test" ve "dev" ortamları için, "app_test.php" ve "app_dev.php" dosyalarında bulunan aşağıdaki satırları iptal edin.


header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');

Not


Eğer symfony uygulamanız tarayıcı içinde yavaş çalışırsa, "cache" ve "logs" klasörlerini paylaşılan klasörde tutmayın (sadece kutu içindeki bir klasörde tutun). Daha sonra "AppKernel.php" dosyasını gerekli şekilde yenileyin. Bu işlemin nasıl yapıldığına dair örnekleri internette kolayca bulabilirsiniz.