Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save przbadu/6de938c0e3ee569162007cff46242a5b to your computer and use it in GitHub Desktop.

Select an option

Save przbadu/6de938c0e3ee569162007cff46242a5b to your computer and use it in GitHub Desktop.
PHP, mysql, apache2 setup for ubuntu 16.04

Install required packages including PHP 7 and 5.6, apache2 and mysql

# Install git
sudo apt-get install git vim

# Add ondrej/php ppa
sudo apt-add-repository -y ppa:ondrej/php

# update package
sudo apt-get update -y

# Install php 7 and or 5.6, mysql-server, apache2, and required packages
sudo apt-get -y install php7.0 php5.6-mysql php5.6-cli php5.6-curl \
php5.6-json php5.6-sqlite3 php5.6-mcrypt php5.6-curl php-xdebug \
php5.6-mbstring libapache2-mod-php5.6 libapache2-mod-php7.0 mysql-server-5.7 apache2

# Once we have PHP 5.6 (or 5.5) and 7.0 installed, 
# we can easily switch between them from the shell using these commands. 

# To enable PHP 5.6 (and disable PHP 7.0) use this command:
sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php

# Similarly, to switch from PHP 5.6 to PHP 7.0, use this command:
sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php

# Even better, we can set an alias in our bash file
# .zshrc for (zsh terminal) and .bashrc for (bash terminal, this is default in ubuntu)
vi ~/.bashrc
alias phpv5='sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php'
alias phpv7='sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php'

Restart apache server

We can restart apache simply by running sudo service apache2 restart

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo service apache2 restart

Install Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Install Laravel

cd /var/www
chown -R www-data.www-data /var/www/
git clone https://github.com/laravel/laravel.git
chmod -R 755 /var/www/laravel
cd laravel
[sudo] composer install

Set Encryption Key

php artisan key:generate

NOTE: if you get any errors, you can try composer update --no-scripts to fix it.

Create apache VirtualHost

I love vim editor, you can use your preferred editor

sudo vi /etc/apache2/sites-available/laravel.example.com.conf

<VirtualHost *:80>
        ServerName laravel.example.com
        DocumentRoot /var/www/laravel/public

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/laravel>
                AllowOverride All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# enable lavarel.example.com VirtualHost
a2ensite laravel.example.com
sudo serivce apache2 reload

Setup /etc/hosts to expose laravel.example.com

sudo echo "127.0.0.1  laravel.example.com" >> /etc/hosts

All the setups are now done, you can visit http://laravel.example.com/ in your browser and it should work.

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