├── readme.md └── ubuntu ├── apache.md ├── bare └── apache.md ├── files ├── apache-default └── nginx-default └── nginx.md /readme.md: -------------------------------------------------------------------------------- 1 | # The VPS Guide for Laravel 4 2 | 3 | After a year of creating Laravel projects, I have found that there is a shortage on how to configure Servers for your Applications. 4 | 5 | Though, there are guides spread all over the internet on setting up PHP servers, this guide looks to be an easy to follow single source for installing everything you need to get started with a lean production server for Laravel 4 (and any PHP Composer project you may want to run). 6 | 7 | These guides are also meant to give some explination as to why certain technologies are being installed. I am including this so that you can become a better dev-op along with me! 8 | 9 | This repository looks to have configuration guides for multiple OS from start to deploy. If you would like to contribute to the project, open an issue or pull request. 10 | 11 | ## Available Guides 12 | 13 | - Ubuntu 12.10 14 | - [Apache2](ubuntu/apache.md) 15 | - [Nginx](ubuntu/nginx.md) 16 | -------------------------------------------------------------------------------- /ubuntu/apache.md: -------------------------------------------------------------------------------- 1 | # Laravel 4 with Apache on Ubuntu 12.10 2 | 3 | To get started with this guide, I assume you have a running installation of Ubuntu with terminal access (either through SSH to a remote server/VPS or on a terminal prompt in a virtual/physical machine). 4 | 5 | For those who are impatient, you can see just the commands for setting up the LAMP stack, composer and Laravel. Find the commands [here](bare/apache.md) 6 | 7 | ## Contents 8 | 9 | - [Initial Setup](#initial-setup) 10 | - [Install Git](#install-git) 11 | - [Setup ZSH Prompt](#setup-zsh-optional) 12 | - [Install LAMP](#install-the-lamp-stack) 13 | - [Install Composer](#installing-composer-globally) 14 | - [Install Laravel](#installing-laravel-4) 15 | - [Setup Apache for Laravel](#setting-up-apache-for-laravel-4) 16 | - [Remote Git Repositories](#pulling-remote-git-repositories) 17 | 18 | ## Initial Setup 19 | 20 | This will guide you through setting up your users and making your SSH access a bit more secure. 21 | 22 | Login to your shell if you are using a remote server you will need to login via SSH `ssh root@**IP_ADDRESS**`. 23 | 24 | Next you should change your root password to something secure. 25 | `passwd` Type in your new password at the prompt. 26 | 27 | Now we should create a custom user to make things a bit more secure (doing everything as the root user is dangerous). `adduser **USER_NAME**` 28 | 29 | Now we need to give your new user some permissions as an administrator. `visudo` Then find the line that looks like `root ALL=(ALL:ALL) ALL`. After this line add a line with `**USER_NAME** ALL=(ALL:ALL) ALL` 30 | 31 | Finally, we will modify some of the properties to allow this new user to login via SSH, and we will change some settings to dissuade hacking. `nano /etc/ssh/sshd_config` 32 | 33 | Find the line that has `Port 22` change this number to any valid port number you would like, this will add some security as now we won't be using the default port, making it harder to hack in. 34 | 35 | Find the line that starts with `Protocol ` and make sure that it is set to `Protocol 2`. This is the SSH protocol version and allows for more secure and stable connections. 36 | 37 | Find the line that lists `PermitRootLogin yes`. Change this to `PermitRootLogin no`. This makes it so that hackers cannot get direct root access without knowing your newly created user credentials. 38 | 39 | Find the line that lists `UseDNS yes`. Change this to `UseDNS no` Though this provides minimal security, this stops hackers from being able to use your domain name to attack your server. This also means that you will have to SSH in using the IP address of your server. 40 | 41 | At the end of this file add a line with `AllowUsers **USER_NAME**` This allows your newly created user to login via SSH. 42 | 43 | Save this file. To wrap things up, you will need to restart SSH `reload ssh`. 44 | 45 | Login to your server using your new credentials. 46 | 47 | ## Install Git 48 | 49 | This is a quick command `sudo apt-get update && sudo apt-get install git`. You will also want to set your name and email for your git config: `git config --global user.name "**USER_NAME**"` & `git config --global user.email **your@email.com***` 50 | 51 | ## Setup ZSH (Optional) 52 | 53 | Having a boring console isn't very fun. ZSH allows for better tab completion than standard Bash shell included in Ubuntu. The best part of ZSH is that it is a subset of Bash (meaning it will run any standard commands Bash will). 54 | 55 | Install ZSH `sudo apt-get install zsh`. 56 | 57 | Next you will want to install Oh-My-ZSH which gives you some good off of the shelf configuration for ZSH. `curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh` 58 | 59 | Now you have to actually make Oh My ZSH your configuration framework. `cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc`. 60 | 61 | You can modify the theme used by changing the ZSH_THEME in your .zshrc file. `nano ~/.zshrc`. I like a theme called 'doubleend' from [Andrew Burgess](https://github.com/andrew8088/oh-my-zsh/blob/master/themes/doubleend.zsh-theme). You will need to download this as it isn't included in the standard Oh My ZSH themes. `cd ~/.oh-my-zsh/themes` then `wget https://raw.github.com/andrew8088/oh-my-zsh/master/themes/doubleend.zsh-theme`. 62 | 63 | Last, we will need to change your default shell prompt from Bash to ZSH. `chsh -s /bin/zsh`. And to get all the awesomeness of ZSH you will have to re-login. 64 | 65 | ## Install the LAMP Stack 66 | 67 | Now we get to the fun part: installing the LAMP (Linux Apache MySQL PHP) components. 68 | 69 | First we install the Apache HTTP Server. This will connect HTTP requests to the proper services (in our case, PHP). `sudo apt-get install apache2`. 70 | 71 | Now we'll tackle the 'M' - MySQL. If you don't already know, MySQL is an Open Source Database that will store the majority of dynamic info for your application. This script is a bit longer because it also installs necessary modules to make MySQL talk to Apache and PHP. This will ask for a root user password: set it to something secure, this user will have access to all of your databases (including the database that controls user authentication for MySQL). `sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql` 72 | 73 | Now we have to finish setting up MySQL. `sudo mysql_install_db` & `sudo /usr/bin/mysql_secure_installation` Answer 'y' to all of the questions. This will set some default settings that make your MySQL install a bit more secure (like disabling remote root login). 74 | 75 | Finally, this is what you all been waitin' for 'aint it? What people pay paper for... Now we're going to install PHP. This isn't your grandad's (or your shared host's) PHP 5.2 or anything either, at the time of this guide, this will install PHP 5.4.6 (if you are on Ubuntu 12.04, you'll get 5.3.x which isn't very fun)! And this will handle installing the PHP MCrypt module which is used in Laravel's Hash. `sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt` 76 | 77 | ## Installing Composer Globally 78 | 79 | For Laravel 4, you're going to need Composer. Don't worry, it is super easy to install! `curl -sS https://getcomposer.org/installer | php` & `sudo mv composer.phar /usr/local/bin/composer` 80 | 81 | Now when you run the command `composer` you should get a composer options prompt. 82 | 83 | ## Installing Laravel 4 84 | 85 | With Laravel 4 being stable you can now install it in a single command, which is awesome. But first we will have to go to the right directory. When you installed your LAMP stack, it created a folder for your websites. `cd /var/www/` 86 | 87 | You will also have to set the permissions for this folder `sudo chmod -R 777 /var/www/` This will allow you to read and write to the websites folder and it will let Apache read from here. 88 | 89 | Now we can pull down Laravel 4 from composer `composer create-project laravel/laravel`. 90 | 91 | We will move into the Laravel installation now `cd laravel`. 92 | 93 | Finally, you need to set the permissions for Laravel to do all of its caching magic: `sudo chmod -R 777 app/storage` 94 | 95 | ## Setting Up Apache for Laravel 4 96 | 97 | By default, Apache reads from `/var/www` which isn't what we want. So we will have to modify our Apache configuration. 98 | 99 | First we have to let Apache know to look for PHP files. `sudo nano /etc/apache2/mods-enabled/dir.conf` 100 | 101 | In this file, find the line that starts with DirectoryIndex and add `index.php` so it will look something like this `DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm` **Note:** The order of these matter, by placing index.php first, we prioritize it over other index files that may exist. Now save the file. 102 | 103 | Now we will modify our default site configuration in Apache. This gets into some more complicated configuration options so I won't be able to explain everything, but I will get you up and running. `sudo nano /etc/apache2/sites-available/default` I suggest using ZSH or Bash tab completion to make sure that you are editing the proper file (sometimes it isn't always named 'default'). 104 | 105 | Now let's go setting by setting to get everything working. 106 | 107 | - VirtualHost - Directs Apache what it needs to listen to *:80 catches all HTTP requests. Every option in this tag will apply to requests that match this pattern and port. 108 | - ServerAdmin - list your email 109 | - DocumentRoot - `/var/www/laravel/public` (This is where we installed Laravel) 110 | - Directory - This will be a XML-ish block and you should make it look like `` 111 | - `Options Indexes FollowSymLinks MultiViews` 112 | - Indexes - Makes Apache look for index.php (and all of the various extensions we set up earlier) 113 | - FollowSymLinks - Allows Unix style symbolic links to be used to put content on your site 114 | - MultiViews - I think this allows Laravel to capture the URIs. I'm not too clear on what it does, but you need this option enabled. 115 | - `AllowOverride None` - .htaccess files will not be allowed to override configuration options, just add options and modules. 116 | - `Order allow,deny` - Makes the server accept requests 117 | - `allow from all` - Makes the server accept requests from any client 118 | 119 | The rest of this file should be left to its defaults so the final product looks a bit like [this](files/apache-default). 120 | 121 | Now we just need to restart the apache service for these settings to be applied `sudo service apache2 restart`. 122 | 123 | That's it! You should now have a raw version of Laravel 4 installed on your VPS and responding like a champ. 124 | 125 | ## Pulling Remote Git Repositories 126 | 127 | Having a bare Laravel install is great, but doing all of your editing through SSH on a VPS is not realistic. Instead, let's wire this up to pull from an existing project you have setup on github or another git repo! 128 | 129 | Move into the websites directory: `cd /var/www` 130 | Remove the old Laravel 4 install `rm -rf laravel` 131 | Pull in your repository `git pull **url_to_repo** laravel` 132 | Composer install components `cd laravel` & `composer install` 133 | Make git ignore file permissions (saves tons of headaches of detached HEAD problems) - git config core.filemode false 134 | Set file permissions `sudo chmod -R 777 app/storage` 135 | 136 | That's it. Now when you want to pull down changes from your repository just run `cd /var/www/laravel` & `git pull` (if you change any composer packages you will also need to run `composer update`). 137 | -------------------------------------------------------------------------------- /ubuntu/bare/apache.md: -------------------------------------------------------------------------------- 1 | **Warning, this file assumes a secure SSH connection and puts default configuration on everything** 2 | 3 | ```bash 4 | sudo apt-get update 5 | sudo apt-get install git 6 | git config --global user.name "**USER_NAME**" 7 | git config --global user.email **your@email.com** 8 | 9 | sudo apt-get install apache2 10 | sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql 11 | sudo mysql_install_db 12 | sudo /usr/bin/mysql_secure_installation 13 | 14 | sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt 15 | 16 | curl -sS https://getcomposer.org/installer | php 17 | sudo mv composer.phar /usr/local/bin/composer 18 | 19 | cd /var/www/ 20 | sudo chmod -R 777 /var/www/ 21 | composer create-project laravel/laravel 22 | cd laravel 23 | sudo chmod -R 777 app/storage 24 | sudo wget https://raw.github.com/rtablada/laravel-vps-guide/master/ubuntu/files/apache-default 25 | sudo mv apache-default /etc/apache2/sites-available/default 26 | sudo service apache2 restart 27 | -------------------------------------------------------------------------------- /ubuntu/files/apache-default: -------------------------------------------------------------------------------- 1 | 2 | ServerAdmin ryan.tablada@gmail.com 3 | 4 | 5 | DocumentRoot /var/www/laravel/public 6 | 7 | 8 | Options Indexes FollowSymLinks MultiViews 9 | AllowOverride None 10 | Order allow,deny 11 | allow from all 12 | 13 | 14 | ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 15 | 16 | AllowOverride None 17 | Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch 18 | Order allow,deny 19 | Allow from all 20 | 21 | 22 | ErrorLog ${APACHE_LOG_DIR}/error.log 23 | 24 | # Possible values include: debug, info, notice, warn, error, crit, 25 | # alert, emerg. 26 | LogLevel warn 27 | 28 | CustomLog ${APACHE_LOG_DIR}/access.log combined 29 | 30 | -------------------------------------------------------------------------------- /ubuntu/files/nginx-default: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80 3 | 4 | server_name localhost 5 | 6 | root /var/www/laravel; 7 | 8 | index index.php; 9 | 10 | location / { try_files $uri $uri/ /index.php?$query_string; } 11 | 12 | if (!-d $request_filename) { rewrite ^/(.+)/$ /$1 permanent; } 13 | 14 | location ~* \.php$ { 15 | fastcgi_pass unix:/var/run/php5-fpm.sock; 16 | fastcgi_index index.php; 17 | fastcgi_split_path_info ^(.+\.php)(.*)$; 18 | include /etc/nginx/fastcgi_params; 19 | fastcgi_params SCRIPT_FILENAME $document_root$fastcgi_script_name; 20 | } 21 | 22 | location ~ /\.ht { deny all; } 23 | 24 | location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ { expires 365d; } 25 | } 26 | -------------------------------------------------------------------------------- /ubuntu/nginx.md: -------------------------------------------------------------------------------- 1 | # Laravel 4 with Apache on Ubuntu 12.10 2 | 3 | To get started with this guide, I assume you have a running installation of Ubuntu with terminal access (either through SSH to a remote server/VPS or on a terminal prompt in a virtual/physical machine). 4 | 5 | ## Contents 6 | 7 | - [Initial Setup](#initial-setup) 8 | - [Install Git](#install-git) 9 | - [Setup ZSH Prompt](#setup-zsh-optional) 10 | - [Install LEMP](#install-the-lemp-stack) 11 | - [Install Composer](#installing-composer-globally) 12 | - [Install Laravel](#installing-laravel-4) 13 | - [Setup Nginx for Laravel](#setting-up-nginx-for-laravel-4) 14 | - [Remote Git Repositories](#pulling-remote-git-repositories) 15 | 16 | ## Initial Setup 17 | 18 | This will guide you through setting up your users and making your SSH access a bit more secure. 19 | 20 | Login to your shell if you are using a remote server you will need to login via SSH `ssh root@**IP_ADDRESS**`. 21 | 22 | Next you should change your root password to something secure. 23 | `passwd` Type in your new password at the prompt. 24 | 25 | Now we should create a custom user to make things a bit more secure (doing everything as the root user is dangerous). `adduser **USER_NAME**` 26 | 27 | Now we need to give your new user some permissions as an administrator. `visudo` Then find the line that looks like `root ALL=(ALL:ALL) ALL`. After this line add a line with `**USER_NAME** ALL=(ALL:ALL) ALL` 28 | 29 | Finally, we will modify some of the properties to allow this new user to login via SSH, and we will change some settings to dissuade hacking. `nano /etc/ssh/sshd_config` 30 | 31 | Find the line that has `Port 22` change this number to any valid port number you would like, this will add some security as now we won't be using the default port, making it harder to hack in. 32 | 33 | Find the line that starts with `Protocol ` and make sure that it is set to `Protocol 2`. This is the SSH protocol version and allows for more secure and stable connections. 34 | 35 | Find the line that lists `PermitRootLogin yes`. Change this to `PermitRootLogin no`. This makes it so that hackers cannot get direct root access without knowing your newly created user credentials. 36 | 37 | Find the line that lists `UseDNS yes`. Change this to `UseDNS no` Though this provides minimal security, this stops hackers from being able to use your domain name to attack your server. This also means that you will have to SSH in using the IP address of your server. 38 | 39 | At the end of this file add a line with `AllowUsers **USER_NAME**` This allows your newly created user to login via SSH. 40 | 41 | Save this file. To wrap things up, you will need to restart SSH `reload ssh`. 42 | 43 | Login to your server using your new credentials. 44 | 45 | ## Install Git 46 | 47 | This is a quick command `sudo apt-get update && sudo apt-get install git`. You will also want to set your name and email for your git config: `git config --global user.name "**USER_NAME**"` & `git config --global user.email **your@email.com***` 48 | 49 | ## Setup ZSH (Optional) 50 | 51 | Having a boring console isn't very fun. ZSH allows for better tab completion than standard Bash shell included in Ubuntu. The best part of ZSH is that it is a subset of Bash (meaning it will run any standard commands Bash will). 52 | 53 | Install ZSH `sudo apt-get install zsh`. 54 | 55 | Next you will want to install Oh-My-ZSH which gives you some good off of the shelf configuration for ZSH. `curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh` 56 | 57 | Now you have to actually make Oh My ZSH your configuration framework. `cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc`. 58 | 59 | You can modify the theme used by changing the ZSH_THEME in your .zshrc file. `nano ~/.zshrc`. I like a theme called 'doubleend' from [Andrew Burgess](https://github.com/andrew8088/oh-my-zsh/blob/master/themes/doubleend.zsh-theme). You will need to download this as it isn't included in the standard Oh My ZSH themes. `cd ~/.oh-my-zsh/themes` then `wget https://raw.github.com/andrew8088/oh-my-zsh/master/themes/doubleend.zsh-theme`. 60 | 61 | Last, we will need to change your default shell prompt from Bash to ZSH. `chsh -s /bin/zsh`. And to get all the awesomeness of ZSH you will have to re-login. 62 | 63 | ## Install the LEMP Stack 64 | 65 | A LEMP stack is a light-weight solution compared to the weight of Apache. For this stack we will be installing Nginx (pronounced engine-x), MySQL, and PHP FPM (a web optimized light-weight build of PHP). 66 | 67 | Let's start by installing Nginx. `sudo apt-get install nginx` 68 | 69 | Now we'll tackle the 'M' - MySQL. If you don't already know, MySQL is an Open Source Database that will store the majority of dynamic info for your application. This script is a bit longer because it also installs necessary modules to make MySQL talk to Apache and PHP. This will ask for a root user password: set it to something secure, this user will have access to all of your databases (including the database that controls user authentication for MySQL). `sudo apt-get install mysql-server`. 70 | 71 | To finish off our LEMP stack, we have to get PHP FPM up and running. `sudo apt-get install php5-fpm` 72 | 73 | Even though PHP FPM gives performance enhancements for our page rendering, it does not handle command line scripts very well without configuration that would be out of the scope of this guide. So we are going to install regular PHP alongside of PHP FPM. `sudo apt-get install php5 php5-mcrypt` 74 | 75 | ## Installing Composer Globally 76 | 77 | For Laravel 4, you're going to need Composer. Don't worry, it is super easy to install! `curl -sS https://getcomposer.org/installer | php` & `sudo mv composer.phar /usr/local/bin/composer` 78 | 79 | Now when you run the command `composer` you should get a composer options prompt. 80 | 81 | ## Installing Laravel 4 82 | 83 | With Laravel 4 being stable you can now install it in a single command, which is awesome. But first we will have to go to the right directory. When you installed your LAMP stack, it created a folder for your websites. `cd /var/www/` 84 | 85 | You will also have to set the permissions for this folder `sudo chmod -R 777 /var/www/` This will allow you to read and write to the websites folder and it will let Apache read from here. 86 | 87 | Now we can pull down Laravel 4 from composer `composer create-project laravel/laravel`. 88 | 89 | We will move into the Laravel installation now `cd laravel`. 90 | 91 | Finally, you need to set the permissions for Laravel to do all of its caching magic: `sudo chmod -R 777 app/storage` 92 | 93 | ## Setting Up Nginx for Laravel 4 94 | 95 | Now we can configure Nginx to serve up our application. We will need to dig into the default site. This is based on the configuration from [Dayle Rees](https://github.com/daylerees/laravel-website-configs/blob/master/nginx.conf) `sudo nano /etc/nginx/sites-available/default` 96 | 97 | - server - this contains all of the rules for a single service. This is similar to VirtualHost block in Apache. 98 | - `listen 80` - this makes Nginx catch all website requests 99 | - `server localhost` - this tells Nginx what to listen to. You can set this to your Fully Qualified Domain or your public IP Address. 100 | - `root /var/www/laravel;` - This is where our project is located 101 | - `index index.php;` - What should we load when we get a raw domain or IP? 102 | - `location /` - this block replicates the .htaccess that ships with Laravel and removes index.php from the application request 103 | - `try_files $uri $uri/ /index.php?$query_string;` 104 | - `if (!-d $request_filename) {rewrite ^/(.+)/$ /$1 permanent; }` - Dayle says this fixes issues for the routing system by removing trailing slashes 105 | - `location ~* \.php$` - This is all of the instructions for PHP files, we are going to send everything to PHP FPM 106 | - `fastcgi_pass unix:/var/run/php5-fpm.sock;` 107 | - `fastcgi_index index.php;` 108 | - `fastcgi_split_path_info ^(.+\.php)(.*)$;` 109 | - `include /etc/nginx/fastcgi_params;` 110 | - `fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;` 111 | - `location ~ /\.ht { deny all; }` - stops the default .htaccess from messing things up 112 | - `location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {expires 365d; }` - This has to do with the expiration of caching for assets 113 | 114 | Your file should look a bit like [this](files/nginx-default). 115 | 116 | Finally, you need to start up Nginx: `sudo service nginx start`. If it says that Nginx is already running then: `sudo service nginx restart`. 117 | 118 | ## Pulling Remote Git Repositories 119 | 120 | Having a bare Laravel install is great, but doing all of your editing through SSH on a VPS is not realistic. Instead, let's wire this up to pull from an existing project you have setup on github or another git repo! 121 | 122 | Move into the websites directory: `cd /var/www` 123 | Remove the old Laravel 4 install `rm -rf laravel` 124 | Pull in your repository `git pull **url_to_repo** laravel` 125 | Composer install components `cd laravel` & `composer install` 126 | Make git ignore file permissions (saves tons of headaches of detached HEAD problems) - git config core.filemode false 127 | Set file permissions `sudo chmod -R 777 app/storage` 128 | 129 | That's it. Now when you want to pull down changes from your repository just run `cd /var/www/laravel` & `git pull` (if you change any composer packages you will also need to run `composer update`). 130 | --------------------------------------------------------------------------------