dexfs
12/30/2017 - 9:34 PM

PHP 7.1 + nginx + MongoDB driver for PHP

PHP 7.1 + nginx + MongoDB driver for PHP

#!/bin/bash 

######################################################
### README ###########################################
######################################################
###
### One-line server install script for Ubuntu.
### Installs Nginx + PHP7.1 + MongoDB for PHP driver.
###
### Removes previous Apache, PHP, nginx installations.
### What it installs:
### - latest nginx with production-ready config
### - PHP 7.1 with mbstring, mcrypt, ext-dom etc
### - MongoDB driver for PHP 7.1
### - composer
###
### USAGE: ###########################################
### wget goo.gl/tKkMW1 -O ubuntu-script.sh && sudo chmod +x ubuntu-script.sh && sudo ./ubuntu-script.sh && rm ubuntu-script.sh
###
### Last updated: Nov 03, 2017

### Let's roll!

#####################################################
### Fix host problem that occurs by default #########

clear
sudo echo -n "127.0.0.1 " > _tmp
sudo cat /etc/hostname >> _tmp
cat /etc/hosts >> _tmp
sudo mv _tmp /etc/hosts

### Force removal of old Apache, PHP, nginx #########

sudo apt-get remove apache* php* nginx* -y

### Add all repositories ############################

echo -e "\n### Adding nginx repository #############\n";
sudo add-apt-repository ppa:nginx/stable -y
echo -e "\n### Adding PHP repository ###############\n";
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update && sudo apt-get upgrade -y

### Install nginx ###################################

echo -e "\n### Installing nginx #############\n";
sudo apt-get update
sudo apt-get install nginx -y

### Use proper nginx config file ####################
### Automatically inserts server IP address #########

echo -e "\n### Setting up nginx config ######\n";
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/_original
wget https://goo.gl/hiChRy -O _nginx_default
currentip=$(wget http://ipinfo.io/ip -qO -)
sed -i s/localhost/${currentip}/g _nginx_default
sudo cp _nginx_default /etc/nginx/sites-available/default
sudo service nginx restart

### Install PHP 7.1 #################################

echo -e "\n### Installing PHP 7.1 #############\n";
sudo apt-get install php7.1 php7.1-fpm php7.1-cli php7.1-common -y
sudo apt-get install php-pear -y
sudo apt-get install php-dev -y

### Install PHP extensions required by Laravel

echo -e "\n### Installing PHP extensions ######\n";
sudo apt-get install php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip php7.1-xml -y

### Install MongoDB driver for PHP 7.1 ##############

echo -e "\n### Installing MongoDB PHP driver ##\n";
sudo apt-get install php-mongodb -y

# Make sure extension=mongodb.so is present in CLI

PhpConfigFile="/etc/php/7.1/cli/php.ini"
if grep -q mongodb.so "$PhpConfigFile"; then
  echo "extension=mongodb.so already found in CLI, dont add."
else
  echo "extension=mongodb.so" | sudo tee "$PhpConfigFile"
  echo "Added extension=mongodb.so to $PhpConfigFile"
fi

# Make sure extension=mongodb.so is present in FPM

PhpConfigFile="/etc/php/7.1/fpm/php.ini"
if grep -q mongodb.so "$PhpConfigFile"; then
  echo "extension=mongodb.so already found in FPM, dont add."
else
  echo "extension=mongodb.so" | sudo tee "$PhpConfigFile"
  echo "Added extension=mongodb.so to $PhpConfigFile"
fi

#echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

sudo service php7.1-fpm restart

### Install composer ################################

echo -e "\n### Installing composer ##\n";
sudo apt-get install composer -y

### Create shortcut to www directory in home ########

sudo ln -s /var/www/html www
sudo ln -s /etc/nginx/sites-available/default nginx-config

### Ensure www is fully writable ####################

currentuser=$(whoami)
sudo chmod 777 -R /var/www
sudo chown ${currentuser}:${currentuser} -R /var/www

### Install unzip and 7zip ##########################

sudo apt install p7zip-full unzip -y

### Clean up ########################################

rm _tmp
rm _nginx_default

### Force removing Apache ###########################

sudo apt-get remove apache* -y

### Update and upgrade ##############################

sudo apt-get update && sudo apt-get upgrade -y

### Verify installs #################################

echo -e ""
echo -e "###############################"
echo -e "### VERIFY THE OUTPUT BELOW ###"
echo -e "###############################"

echo -e "\n### nginx #############\n"
sudo service nginx restart && sudo service nginx configtest
echo -e "$(nginx -v)"

echo -e "\n### PHP ###############\n"
echo -e "$(php -v)"

echo -e "\n### MongoDB for PHP ###\n"
echo -e "$(php -i | grep mongo)\n"

echo -e "\n### Finished ###"