|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script references the following script. |
| 4 | +# https://github.com/laravel/settler/blob/master/scripts/provision.sh |
| 5 | +# |
| 6 | +#-------------------------------------------------------------------------- |
| 7 | +# Before run this script... |
| 8 | +#-------------------------------------------------------------------------- |
| 9 | +# |
| 10 | +# Get sudo permission. |
| 11 | +# user@server:~$ sudo -s |
| 12 | +# |
| 13 | +# Add User and group. |
| 14 | +# user@server:~# adduser deployer |
| 15 | +# user@server:~# usermod -G www-data deployer |
| 16 | +# user@server:~# id deployer |
| 17 | +# user@server:~# groups www-data |
| 18 | +# |
| 19 | +# TROUBLESHOOTING. |
| 20 | +# |
| 21 | +# If you encounter error message like "sudo: no tty present |
| 22 | +# and no askpass program specified ...", you can work around this error |
| 23 | +# by adding the following line on your production server's /etc/sudoers. |
| 24 | +# |
| 25 | +# user@server:~# visudo |
| 26 | +# |
| 27 | +# Add following lines to the file and save. |
| 28 | +# |
| 29 | +# deployer ALL=(ALL:ALL) NOPASSWD: ALL |
| 30 | +# %www-data ALL=(ALL:ALL) NOPASSWD:/usr/sbin/service php7.0-fpm restart,/usr/sbin/service nginx restart |
| 31 | +# |
| 32 | +#-------------------------------------------------------------------------- |
| 33 | +# How to run |
| 34 | +#-------------------------------------------------------------------------- |
| 35 | +# |
| 36 | +# user@server:~# bash provision.sh deployer password | tee log.txt |
| 37 | +# |
| 38 | + |
| 39 | +if [[ -z "$1" ]] |
| 40 | +then |
| 41 | + echo "Error: missing required parameters." |
| 42 | + echo "Usage: " |
| 43 | + echo " ./provision username" |
| 44 | + exit |
| 45 | +fi |
| 46 | + |
| 47 | +export DEBIAN_FRONTEND=noninteractive |
| 48 | +USERNAME=$1 |
| 49 | +PASSWD=$2 |
| 50 | + |
| 51 | +# Update Package List |
| 52 | + |
| 53 | +apt-get update |
| 54 | + |
| 55 | +# Update System Packages |
| 56 | + |
| 57 | +apt-get -y upgrade |
| 58 | + |
| 59 | +# Force Locale |
| 60 | + |
| 61 | +echo "LC_ALL=en_US.UTF-8" >> /etc/default/locale |
| 62 | +locale-gen en_US.UTF-8 |
| 63 | + |
| 64 | +# Install Some PPAs |
| 65 | + |
| 66 | +apt-get install -y software-properties-common curl |
| 67 | + |
| 68 | +apt-add-repository ppa:nginx/stable -y |
| 69 | +#apt-add-repository ppa:rwky/redis -y |
| 70 | +apt-add-repository ppa:ondrej/php -y |
| 71 | + |
| 72 | +# gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported |
| 73 | +apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 5072E1F5 |
| 74 | +sh -c 'echo "deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-5.7" >> /etc/apt/sources.list.d/mysql.list' |
| 75 | + |
| 76 | +# Update Package Lists |
| 77 | + |
| 78 | +apt-get update |
| 79 | + |
| 80 | +# Install Some Basic Packages |
| 81 | + |
| 82 | +apt-get install -y --force-yes \ |
| 83 | + build-essential \ |
| 84 | + dos2unix \ |
| 85 | + gcc \ |
| 86 | + git \ |
| 87 | + libmcrypt4 \ |
| 88 | + libpcre3-dev \ |
| 89 | + make \ |
| 90 | + python2.7-dev \ |
| 91 | + python-pip \ |
| 92 | + re2c \ |
| 93 | + supervisor \ |
| 94 | + unattended-upgrades \ |
| 95 | + whois \ |
| 96 | + libnotify-bin; |
| 97 | + |
| 98 | +# Set My Timezone |
| 99 | + |
| 100 | +# ln -sf /usr/share/zoneinfo/UTC /etc/localtime |
| 101 | + |
| 102 | +# Install PHP Stuffs |
| 103 | + |
| 104 | +apt-get install -y --force-yes \ |
| 105 | + php7.0-cli \ |
| 106 | + php7.0-dev \ |
| 107 | + php-gd \ |
| 108 | + php-apcu \ |
| 109 | + php-curl \ |
| 110 | + php7.0-mcrypt \ |
| 111 | + php-mysql \ |
| 112 | + php-memcached \ |
| 113 | + php7.0-readline \ |
| 114 | + php-mbstring \ |
| 115 | + php-xml \ |
| 116 | + php7.0-zip \ |
| 117 | + php7.0-intl \ |
| 118 | + php7.0-bcmath; |
| 119 | + |
| 120 | +# php-sqlite3 \ |
| 121 | +# php-pgsql \ |
| 122 | +# php-imap \ |
| 123 | +# php-xdebug \ |
| 124 | + |
| 125 | +# Install Composer |
| 126 | + |
| 127 | +curl -sS https://getcomposer.org/installer | php |
| 128 | +mv composer.phar /usr/local/bin/composer |
| 129 | + |
| 130 | +# Add Composer Global Bin To Path |
| 131 | + |
| 132 | +printf "\nPATH=\"/home/${USERNAME}/.composer/vendor/bin:\$PATH\"\n" | tee -a /home/${USERNAME}/.profile |
| 133 | +# printf "\nAPP_ENV=production\n" | tee -a /home/${USERNAME}/.profile |
| 134 | + |
| 135 | +# Set Some PHP CLI Settings |
| 136 | + |
| 137 | +sed -i "s/expose_php = .*/expose_php = Off/" /etc/php/7.0/cli/php.ini |
| 138 | +#sed -i "s/error_reporting = .*/error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT/" /etc/php/7.0/cli/php.ini |
| 139 | +sed -i "s/display_errors = .*/display_errors = Off/" /etc/php/7.0/cli/php.ini |
| 140 | +sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.0/cli/php.ini |
| 141 | +sed -i "s/upload_max_filesize = .*/upload_max_filesize = 100M/" /etc/php/7.0/fpm/php.ini |
| 142 | +sed -i "s/post_max_size = .*/post_max_size = 100M/" /etc/php/7.0/fpm/php.ini |
| 143 | +sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.0/cli/php.ini |
| 144 | + |
| 145 | +# Install Nginx & PHP-FPM |
| 146 | + |
| 147 | +apt-get install -y --force-yes nginx php7.0-fpm |
| 148 | + |
| 149 | +rm /etc/nginx/sites-enabled/default |
| 150 | +rm /etc/nginx/sites-available/default |
| 151 | +service nginx restart |
| 152 | + |
| 153 | +# Setup Some PHP-FPM Options |
| 154 | + |
| 155 | +sed -i "s/expose_php = .*/expose_php = Off/" /etc/php/7.0/fpm/php.ini |
| 156 | +#sed -i "s/error_reporting = .*/error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT/" /etc/php/7.0/fpm/php.ini |
| 157 | +sed -i "s/display_errors = .*/display_errors = Off/" /etc/php/7.0/fpm/php.ini |
| 158 | +sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.0/fpm/php.ini |
| 159 | +sed -i "s/upload_max_filesize = .*/upload_max_filesize = 100M/" /etc/php/7.0/fpm/php.ini |
| 160 | +sed -i "s/post_max_size = .*/post_max_size = 100M/" /etc/php/7.0/fpm/php.ini |
| 161 | +sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.0/fpm/php.ini |
| 162 | +sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/7.0/fpm/php.ini |
| 163 | + |
| 164 | +cat > /etc/nginx/fastcgi_params << EOF |
| 165 | +fastcgi_param QUERY_STRING \$query_string; |
| 166 | +fastcgi_param REQUEST_METHOD \$request_method; |
| 167 | +fastcgi_param CONTENT_TYPE \$content_type; |
| 168 | +fastcgi_param CONTENT_LENGTH \$content_length; |
| 169 | +fastcgi_param SCRIPT_FILENAME \$request_filename; |
| 170 | +fastcgi_param SCRIPT_NAME \$fastcgi_script_name; |
| 171 | +fastcgi_param REQUEST_URI \$request_uri; |
| 172 | +fastcgi_param DOCUMENT_URI \$document_uri; |
| 173 | +fastcgi_param DOCUMENT_ROOT \$document_root; |
| 174 | +fastcgi_param SERVER_PROTOCOL \$server_protocol; |
| 175 | +fastcgi_param GATEWAY_INTERFACE CGI/1.1; |
| 176 | +fastcgi_param SERVER_SOFTWARE nginx/\$nginx_version; |
| 177 | +fastcgi_param REMOTE_ADDR \$remote_addr; |
| 178 | +fastcgi_param REMOTE_PORT \$remote_port; |
| 179 | +fastcgi_param SERVER_ADDR \$server_addr; |
| 180 | +fastcgi_param SERVER_PORT \$server_port; |
| 181 | +fastcgi_param SERVER_NAME \$server_name; |
| 182 | +fastcgi_param HTTPS \$https if_not_empty; |
| 183 | +fastcgi_param REDIRECT_STATUS 200; |
| 184 | +EOF |
| 185 | + |
| 186 | +# Set The Nginx & PHP-FPM User |
| 187 | + |
| 188 | +sed -i "s/user www-data;/user ${USERNAME};/" /etc/nginx/nginx.conf |
| 189 | +sed -i "s/# server_names_hash_bucket_size.*/server_names_hash_bucket_size 64;/" /etc/nginx/nginx.conf |
| 190 | + |
| 191 | +sed -i "s/user = www-data/user = ${USERNAME}/" /etc/php/7.0/fpm/pool.d/www.conf |
| 192 | +sed -i "s/group = www-data/group = ${USERNAME}/" /etc/php/7.0/fpm/pool.d/www.conf |
| 193 | + |
| 194 | +sed -i "s/listen\.owner.*/listen.owner = ${USERNAME}/" /etc/php/7.0/fpm/pool.d/www.conf |
| 195 | +sed -i "s/listen\.group.*/listen.group = ${USERNAME}/" /etc/php/7.0/fpm/pool.d/www.conf |
| 196 | +sed -i "s/;listen\.mode.*/listen.mode = 0666/" /etc/php/7.0/fpm/pool.d/www.conf |
| 197 | + |
| 198 | +service nginx restart |
| 199 | +service php7.0-fpm restart |
| 200 | + |
| 201 | +# Add User To WWW-Data |
| 202 | + |
| 203 | +#usermod -a -G www-data $USERNAME |
| 204 | +#id $USERNAME |
| 205 | +#groups www-data |
| 206 | + |
| 207 | +# Install SQLite |
| 208 | + |
| 209 | +#apt-get install -y --force-yes sqlite3 libsqlite3-dev |
| 210 | + |
| 211 | +# Install MySQL |
| 212 | + |
| 213 | +debconf-set-selections <<< "mysql-community-server mysql-community-server/data-dir select ''" |
| 214 | +debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password ${PASSWD}" |
| 215 | +debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password ${PASSWD}" |
| 216 | +apt-get install -y mysql-server |
| 217 | + |
| 218 | +# Configure MySQL Password Lifetime |
| 219 | + |
| 220 | +# echo "default_password_lifetime = 0" >> /etc/mysql/my.cnf |
| 221 | + |
| 222 | +# Configure MySQL Remote Access |
| 223 | + |
| 224 | +sed -i '/^bind-address/s/bind-address.*=.*/bind-address = 0.0.0.0/' /etc/mysql/my.cnf |
| 225 | + |
| 226 | +#mysql --user="root" --password="${PASSWD}" -e "GRANT ALL ON *.* TO ${USERNAME}@'0.0.0.0' IDENTIFIED BY '${PASSWD}' WITH GRANT OPTION;" |
| 227 | +service mysql restart |
| 228 | + |
| 229 | +mysql --user="root" --password="${PASSWD}" -e "CREATE USER '${USERNAME}'@'0.0.0.0' IDENTIFIED BY '${PASSWD}';" |
| 230 | +mysql --user="root" --password="${PASSWD}" -e "GRANT ALL ON *.* TO '${USERNAME}'@'0.0.0.0' IDENTIFIED BY '${PASSWD}' WITH GRANT OPTION;" |
| 231 | +mysql --user="root" --password="${PASSWD}" -e "GRANT ALL ON *.* TO '${USERNAME}'@'%' IDENTIFIED BY '${PASSWD}' WITH GRANT OPTION;" |
| 232 | +mysql --user="root" --password="${PASSWD}" -e "FLUSH PRIVILEGES;" |
| 233 | +#mysql --user="root" --password="${PASSWD}" -e "CREATE DATABASE ${USERNAME};" |
| 234 | +service mysql restart |
| 235 | + |
| 236 | +# Add Timezone Support To MySQL |
| 237 | + |
| 238 | +#mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql --user=root --password=${PASSWD} mysql |
| 239 | + |
| 240 | +# Install A Few Other Things |
| 241 | + |
| 242 | +apt-get install -y --force-yes memcached #beanstalkd redis-server |
| 243 | + |
| 244 | +# Configure Beanstalkd |
| 245 | + |
| 246 | +#sed -i "s/#START=yes/START=yes/" /etc/default/beanstalkd |
| 247 | +#/etc/init.d/beanstalkd start |
| 248 | + |
| 249 | +# Enable Swap Memory |
| 250 | + |
| 251 | +/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 |
| 252 | +/sbin/mkswap /var/swap.1 |
| 253 | +/sbin/swapon /var/swap.1 |
0 commit comments