Skip to content

Instantly share code, notes, and snippets.

@jeweltheme
Forked from EmranAhmed/vps-setup.sh
Created October 26, 2015 08:55
Show Gist options
  • Save jeweltheme/a7a8cf27edc5e637ede2 to your computer and use it in GitHub Desktop.
Save jeweltheme/a7a8cf27edc5e637ede2 to your computer and use it in GitHub Desktop.
Simple and Straightforward VPS ( ubuntu and debian ) setup :)
#!/bin/bash
# Upload vps-setup.sh file
# open terminal and change permission like: chmod +x ./setup.sh
# ./vps-setup.sh
# delete this file after setup :)
COLOR_RESET="\033[33;0m"
COLOR_RED="\033[33;31m"
COLOR_GREEN="\033[33;32m"
COLOR_YELLOW="\033[33;33m"
COLOR_BLUE="\033[33;34m"
COLOR_MAGENTA="\033[33;35m"
COLOR_GRAY="\033[33;30m"
COLOR_CYAN="\033[33;36m"
echo -en "${COLOR_GREEN}Host Name ${COLOR_CYAN}(themehippo)${COLOR_GREEN} : ${COLOR_BLUE}"
read YOUR_HOST_NAME
echo -en "${COLOR_GREEN}Domain Name with ${COLOR_RED}www${COLOR_CYAN} (www.themehippo.com)${COLOR_GREEN} : ${COLOR_BLUE}"
read YOUR_DOMAIN_NAME
#echo -en "${COLOR_GREEN}Non ${COLOR_RED}www${COLOR_GREEN} Domain Name ${COLOR_CYAN} (themehippo.com, themeroxsite.com)${COLOR_GREEN} : ${COLOR_BLUE}"
#read YOUR_NON_WWW_DOMAIN_NAME
YOUR_NON_WWW_DOMAIN_NAME=$(echo $YOUR_DOMAIN_NAME | sed -e "s/www.//g")
echo -en "${COLOR_GREEN}MYSQL Password : ${COLOR_BLUE}"
read YOUR_SQL_PASSWORD
echo -en "\n${COLOR_GREEN}Want to set Host Name Again? ${COLOR_RED}(y/n)${COLOR_GREEN}: ${COLOR_BLUE}"
read WANT_TO_SET_HOSTNAME
echo -en "${COLOR_RESET}"
YOUR_SERVER_IP=$(hostname -I | cut -f1 -d' ') # `hostname -I | cut -f2 -d' '` for vagrant
YOUR_ACCESS_LOG_FILE="access-"$(echo $YOUR_NON_WWW_DOMAIN_NAME | sed -e "s/\./-/g")".log"
YOUR_ERROR_LOG_FILE="error-"$(echo $YOUR_NON_WWW_DOMAIN_NAME | sed -e "s/\./-/g")".log"
if [[ $WANT_TO_SET_HOSTNAME == "y" ]]
then
echo -e "\n${COLOR_YELLOW}Setting Host Name${COLOR_RESET}"
echo "$YOUR_HOST_NAME" > /etc/hostname
hostname -F /etc/hostname
echo "$YOUR_SERVER_IP $YOUR_HOST_NAME" >> /etc/hosts
echo -e "\n${COLOR_YELLOW}===========================> Done. ${COLOR_RESET}"
fi
echo -e "\n${COLOR_YELLOW}Setting TimeZone${COLOR_RESET}\n"
timedatectl set-timezone Asia/Dhaka
echo -e "\n${COLOR_YELLOW}Updating repos${COLOR_RESET}\n"
apt-get update
apt-get upgrade
apt-get install debconf-utils zip unzip -y
echo -e "\n${COLOR_YELLOW}Prepairing for Nginx Installation${COLOR_RESET}\n"
apt-get remove nginx nginx-full nginx-common nginx-core
sudo apt-get autoremove
mkdir /opt/www
mkdir /opt/www/$YOUR_DOMAIN_NAME
wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
rm nginx_signing.key
echo "deb http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list
echo "deb-src http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list
apt-get update
echo -e "\n${COLOR_YELLOW}Installing and Configuring Nginx${COLOR_RESET}\n"
apt-get install nginx -y
service nginx restart
echo "mysql-server mysql-server/root_password password $YOUR_SQL_PASSWORD" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password $YOUR_SQL_PASSWORD" | debconf-set-selections
apt-get install rsync curl vim build-essential python-software-properties git-core php5-fpm php5-common php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-ming php5-ps mysql-server -y > /dev/null
service php5-fpm restart
service mysql restart
/usr/bin/mysql_secure_installation
php5enmod mcrypt
service php5-fpm restart
mkdir /opt/log
touch /opt/log/$YOUR_ACCESS_LOG_FILE
touch /opt/log/$YOUR_ERROR_LOG_FILE
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
cat > /etc/nginx/sites-available/$YOUR_DOMAIN_NAME << "EOF" # cat >> Means Append and cat > write
server {
listen 80;
EOF
# Root Setup
echo " root /opt/www/$YOUR_DOMAIN_NAME;" >> /etc/nginx/sites-available/$YOUR_DOMAIN_NAME
cat >> /etc/nginx/sites-available/$YOUR_DOMAIN_NAME << "EOF"
index index.php index.html index.htm;
EOF
echo " server_name $YOUR_DOMAIN_NAME;" >> /etc/nginx/sites-available/$YOUR_DOMAIN_NAME
echo " access_log /opt/log/$YOUR_ACCESS_LOG_FILE;" >> /etc/nginx/sites-available/$YOUR_DOMAIN_NAME
echo " error_log /opt/log/$YOUR_ERROR_LOG_FILE;" >> /etc/nginx/sites-available/$YOUR_DOMAIN_NAME
cat >> /etc/nginx/sites-available/$YOUR_DOMAIN_NAME << "EOF"
location / {
# autoindex on; # For Directory Listing
try_files $uri $uri/ /index.php?$args;
}
location /phpmyadmin {
root /usr/share;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Aggressive caching for static files
location ~* \.(css|js|html|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
access_log off;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "max-age=31536000, public";
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
}
EOF
ln -sf /etc/nginx/sites-available/$YOUR_DOMAIN_NAME /etc/nginx/sites-enabled/$YOUR_DOMAIN_NAME
cat > /etc/nginx/nginx.conf << "EOF" # cat >> Means Append and cat > write
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
client_max_body_size 100M;
client_body_timeout 600s;
#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
EOF
echo -e "\n${COLOR_YELLOW}Configuring PHP.INI FIle${COLOR_RESET}\n"
sed -i "s/;cgi.fix_pathinfo = .*/cgi.fix_pathinfo = 0/" /etc/php5/fpm/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 100M/" /etc/php5/fpm/php.ini
sed -i "s/post_max_size = .*/post_max_size = 512M/" /etc/php5/fpm/php.ini
sed -i "s/memory_limit = .*/memory_limit = 256M/" /etc/php5/fpm/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php5/fpm/php.ini
service nginx restart
service php5-fpm restart
service mysql restart
echo "phpmyadmin phpmyadmin/app-password-confirm password $YOUR_SQL_PASSWORD" | debconf-set-selections
echo "phpmyadmin phpmyadmin/mysql/admin-pass password $YOUR_SQL_PASSWORD" | debconf-set-selections
echo "phpmyadmin phpmyadmin/mysql/app-pass password $YOUR_SQL_PASSWORD" | debconf-set-selections
echo "phpmyadmin phpmyadmin/reconfigure-webserver multiselect none" | debconf-set-selections
echo "phpmyadmin phpmyadmin/dbconfig-install boolean true" | debconf-set-selections
apt-get install phpmyadmin -y
service nginx restart
service php5-fpm restart
service mysql restart
cat > /opt/www/$YOUR_DOMAIN_NAME/info.php << "EOF"
<?php phpinfo();
EOF
chown -R www-data:www-data /opt/www/$YOUR_DOMAIN_NAME
chmod -R 755 /opt/www/$YOUR_DOMAIN_NAME
echo -e "\n${COLOR_YELLOW}All Done Have Fun :) ${COLOR_RESET}"
echo -e "\n${COLOR_YELLOW}Please check http://${YOUR_SERVER_IP}/info.php and http://${YOUR_SERVER_IP}/phpmyadmin ${COLOR_RESET}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment