Advertisement
Guest User

forge.sh

a guest
Dec 19th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.19 KB | None | 0 0
  1. # Install script for Ubuntu 18.04 (based on forge.laravel.com script, december 2018)
  2.  
  3. # Your data
  4.  
  5. SERVER_NAME="server-name"
  6.  
  7. SERVER_IP="111.222.333.444"
  8.  
  9. PUBLIC_SSH_KEYS="# place content id_rsa.pub here
  10. # key 1
  11. ssh-rsa xxxxxxx user@workstation
  12. # key 2
  13. ssh-rsa xxxxxxx user@notebook"
  14.  
  15. USER="forge"
  16. SUDO_PASSWORD="pass_for_sudo"
  17. MYSQL_ROOT_PASSWORD="pass_for_mysql_root_user"
  18.  
  19. PHP_VERSION="7.1"
  20. PHP_MEMORY_LIMIT="512M"
  21. PHP_TIMEZONE="UTC"
  22.  
  23. GIT_USERNAME=""
  24. GIT_EMAIL=""
  25.  
  26. SWAP_SIZE="1G"
  27.  
  28. SERVER_TIMEZONE="Europe/Moscow" # local timezone for cron
  29.  
  30. # =================== DO NOT CHANGE BELOW =====================================
  31.  
  32. sudo sed -i "s/#precedence ::ffff:0:0\/96  100/precedence ::ffff:0:0\/96  100/" /etc/gai.conf
  33.  
  34. # Upgrade The Base Packages
  35.  
  36. export DEBIAN_FRONTEND=noninteractive
  37.  
  38. apt-get update
  39. apt-get upgrade -y
  40.  
  41. # Add A Few PPAs To Stay Current
  42.  
  43. apt-get install -y --force-yes software-properties-common
  44.  
  45. # apt-add-repository ppa:fkrull/deadsnakes-python2.7 -y
  46. apt-add-repository ppa:nginx/development -y
  47. apt-add-repository ppa:chris-lea/redis-server -y
  48. apt-add-repository ppa:ondrej/apache2 -y
  49. apt-add-repository ppa:ondrej/php -y
  50.  
  51. # Update Package Lists
  52.  
  53. apt-get update
  54.  
  55. # Base Packages
  56.  
  57. add-apt-repository universe
  58.  
  59. apt-get install -y --force-yes build-essential curl fail2ban gcc git libmcrypt4 libpcre3-dev \
  60. make python2.7 python-pip sendmail supervisor ufw unattended-upgrades unzip whois zsh ncdu
  61.  
  62. # Additional useful packages
  63.  
  64. apt-get install -y --force-yes mc p7zip-full htop tmux
  65.  
  66. # Install Python Httpie
  67.  
  68. pip install httpie
  69.  
  70. # Disable Password Authentication Over SSH
  71.  
  72. sed -i "/PasswordAuthentication yes/d" /etc/ssh/sshd_config
  73. echo "" | sudo tee -a /etc/ssh/sshd_config
  74. echo "" | sudo tee -a /etc/ssh/sshd_config
  75. echo "PasswordAuthentication no" | sudo tee -a /etc/ssh/sshd_config
  76.  
  77. # Restart SSH
  78.  
  79. ssh-keygen -A
  80. service ssh restart
  81.  
  82. # Set The Hostname If Necessary
  83.  
  84. echo "$SERVER_NAME" > /etc/hostname
  85. sed -i "s/127\.0\.0\.1.*localhost/127.0.0.1 $SERVER_NAME localhost/" /etc/hosts
  86. hostname $SERVER_NAME
  87.  
  88. # Set The Timezone
  89.  
  90. ln -sf /usr/share/zoneinfo/$SERVER_TIMEZONE /etc/localtime
  91.  
  92. # Create The Root SSH Directory If Necessary
  93.  
  94. if [ ! -d /root/.ssh ]
  95. then
  96.     mkdir -p /root/.ssh
  97.     touch /root/.ssh/authorized_keys
  98. fi
  99.  
  100. # Setup Forge User
  101.  
  102. useradd $USER
  103. mkdir -p /home/$USER/.ssh
  104. mkdir -p /home/$USER/.forge
  105. adduser $USER sudo
  106.  
  107. # Setup Bash For Forge User
  108.  
  109. chsh -s /bin/bash $USER
  110. cp /root/.profile /home/$USER/.profile
  111. cp /root/.bashrc /home/$USER/.bashrc
  112.  
  113. # Set The Sudo Password For Forge
  114.  
  115. PASSWORD=$(mkpasswd $SUDO_PASSWORD)
  116. usermod --password $PASSWORD $USER
  117.  
  118. # Build Formatted Keys & Copy Keys To Forge
  119.  
  120. cat > /root/.ssh/authorized_keys << EOF
  121. $PUBLIC_SSH_KEYS
  122. EOF
  123.  
  124.  
  125. cp /root/.ssh/authorized_keys /home/$USER/.ssh/authorized_keys
  126.  
  127. # Create The Server SSH Key
  128.  
  129. ssh-keygen -f /home/$USER/.ssh/id_rsa -t rsa -N ''
  130.  
  131. # Copy Source Control Public Keys Into Known Hosts File
  132.  
  133. ssh-keyscan -H github.com >> /home/$USER/.ssh/known_hosts
  134. ssh-keyscan -H bitbucket.org >> /home/$USER/.ssh/known_hosts
  135. ssh-keyscan -H gitlab.com >> /home/$USER/.ssh/known_hosts
  136.  
  137. # Configure Git Settings
  138.  
  139. git config --global user.name "$GIT_USERNAME"
  140. git config --global user.email "$GIT_EMAIL"
  141.  
  142. # Setup User Directory Permissions
  143.  
  144. chown -R $USER:$USER /home/$USER
  145. chmod -R 755 /home/$USER
  146. chmod 700 /home/$USER/.ssh/id_rsa
  147.  
  148. # Setup UFW Firewall
  149.  
  150. ufw allow 22
  151. ufw allow 80
  152. ufw allow 443
  153. ufw enable
  154.  
  155. # Allow FPM Restart
  156.  
  157. echo "forge ALL=NOPASSWD: /usr/sbin/service php7.3-fpm reload" > /etc/sudoers.d/php-fpm
  158. echo "forge ALL=NOPASSWD: /usr/sbin/service php7.2-fpm reload" >> /etc/sudoers.d/php-fpm
  159. echo "forge ALL=NOPASSWD: /usr/sbin/service php7.1-fpm reload" >> /etc/sudoers.d/php-fpm
  160. echo "forge ALL=NOPASSWD: /usr/sbin/service php7.0-fpm reload" >> /etc/sudoers.d/php-fpm
  161. echo "forge ALL=NOPASSWD: /usr/sbin/service php5.6-fpm reload" >> /etc/sudoers.d/php-fpm
  162. echo "forge ALL=NOPASSWD: /usr/sbin/service php5-fpm reload" >> /etc/sudoers.d/php-fpm
  163.  
  164. # Install Base PHP Packages
  165.  
  166. apt-get install -y --force-yes php$PHP_VERSION-cli php$PHP_VERSION-dev \
  167. php$PHP_VERSION-pgsql php$PHP_VERSION-sqlite3 php$PHP_VERSION-gd \
  168. php$PHP_VERSION-curl php$PHP_VERSION-memcached \
  169. php$PHP_VERSION-imap php$PHP_VERSION-mysql php$PHP_VERSION-mbstring \
  170. php$PHP_VERSION-xml php$PHP_VERSION-zip php$PHP_VERSION-bcmath php$PHP_VERSION-soap \
  171. php$PHP_VERSION-intl php$PHP_VERSION-readline
  172.  
  173. # Install Composer Package Manager
  174.  
  175. curl -sS https://getcomposer.org/installer | php
  176. mv composer.phar /usr/local/bin/composer
  177.  
  178. # Misc. PHP CLI Configuration
  179.  
  180. sudo sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/$PHP_VERSION/cli/php.ini
  181. sudo sed -i "s/display_errors = .*/display_errors = On/" /etc/php/$PHP_VERSION/cli/php.ini
  182. sudo sed -i "s/memory_limit = .*/memory_limit = $PHP_MEMORY_LIMIT/" /etc/php/$PHP_VERSION/cli/php.ini
  183. sudo sed -i "s/;date.timezone.*/date.timezone = $PHP_TIMEZONE/" /etc/php/$PHP_VERSION/cli/php.ini
  184.  
  185. # Configure Sessions Directory Permissions
  186.  
  187. chmod 733 /var/lib/php/sessions
  188. chmod +t /var/lib/php/sessions
  189.  
  190.    
  191. # Install Nginx & PHP-FPM
  192.  
  193. apt-get install -y --force-yes nginx php$PHP_VERSION-fpm
  194. systemctl enable nginx.service
  195.  
  196. # Generate dhparam File
  197.  
  198. openssl dhparam -out /etc/nginx/dhparams.pem 2048
  199.  
  200. # Tweak Some PHP-FPM Settings
  201.  
  202. sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/$PHP_VERSION/fpm/php.ini
  203. sed -i "s/display_errors = .*/display_errors = On/" /etc/php/$PHP_VERSION/fpm/php.ini
  204. sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/$PHP_VERSION/fpm/php.ini
  205. sed -i "s/memory_limit = .*/memory_limit = $PHP_MEMORY_LIMIT/" /etc/php/$PHP_VERSION/fpm/php.ini
  206. sed -i "s/;date.timezone.*/date.timezone = $PHP_TIMEZONE/" /etc/php/$PHP_VERSION/fpm/php.ini
  207.  
  208. # Configure FPM Pool Settings
  209.  
  210. sed -i "s/^user = www-data/user = forge/" /etc/php/$PHP_VERSION/fpm/pool.d/www.conf
  211. sed -i "s/^group = www-data/group = forge/" /etc/php/$PHP_VERSION/fpm/pool.d/www.conf
  212. sed -i "s/;listen\.owner.*/listen.owner = forge/" /etc/php/$PHP_VERSION/fpm/pool.d/www.conf
  213. sed -i "s/;listen\.group.*/listen.group = forge/" /etc/php/$PHP_VERSION/fpm/pool.d/www.conf
  214. sed -i "s/;listen\.mode.*/listen.mode = 0666/" /etc/php/$PHP_VERSION/fpm/pool.d/www.conf
  215. sed -i "s/;request_terminate_timeout.*/request_terminate_timeout = 60/" /etc/php/$PHP_VERSION/fpm/pool.d/www.conf
  216.  
  217. # Configure Primary Nginx Settings
  218.  
  219. sed -i "s/user www-data;/user forge;/" /etc/nginx/nginx.conf
  220. sed -i "s/worker_processes.*/worker_processes auto;/" /etc/nginx/nginx.conf
  221. sed -i "s/# multi_accept.*/multi_accept on;/" /etc/nginx/nginx.conf
  222. sed -i "s/# server_names_hash_bucket_size.*/server_names_hash_bucket_size 128;/" /etc/nginx/nginx.conf
  223.  
  224. # Configure Gzip
  225.  
  226. cat > /etc/nginx/conf.d/gzip.conf << EOF
  227. gzip_comp_level 5;
  228. gzip_min_length 256;
  229. gzip_proxied any;
  230. gzip_vary on;
  231.  
  232. gzip_types
  233. application/atom+xml
  234. application/javascript
  235. application/json
  236. application/rss+xml
  237. application/vnd.ms-fontobject
  238. application/x-font-ttf
  239. application/x-web-app-manifest+json
  240. application/xhtml+xml
  241. application/xml
  242. font/opentype
  243. image/svg+xml
  244. image/x-icon
  245. text/css
  246. text/plain
  247. text/x-component;
  248.  
  249. EOF
  250.  
  251. # Disable The Default Nginx Site
  252.  
  253. rm /etc/nginx/sites-enabled/default
  254. rm /etc/nginx/sites-available/default
  255. service nginx restart
  256.  
  257. # Install A Catch All Server
  258.  
  259. cat > /etc/nginx/sites-available/catch-all << EOF
  260. server {
  261.     return 404;
  262. }
  263. EOF
  264.  
  265. ln -s /etc/nginx/sites-available/catch-all /etc/nginx/sites-enabled/catch-all
  266.  
  267. cat > /etc/nginx/sites-available/example << EOF
  268. server {
  269.     listen 80;
  270.     server_name DOMAIN.COM;
  271.     root /home/forge/DOMAIN.COM/public;
  272.  
  273.     index index.html index.htm index.php;
  274.  
  275.     charset utf-8;
  276.  
  277.     location / {
  278.         try_files \$uri \$uri/ /index.php?\$query_string;
  279.     }
  280.  
  281.     location = /favicon.ico { access_log off; log_not_found off; }
  282.     location = /robots.txt  { access_log off; log_not_found off; }
  283.  
  284.     access_log off;
  285.     error_log  /var/log/nginx/DOMAIN.COM-error.log error;
  286.  
  287.     error_page 404 /index.php;
  288.  
  289.     location ~ \.php$ {
  290.         fastcgi_split_path_info ^(.+\.php)(/.+)$;
  291.         fastcgi_pass unix:/run/php/php$PHP_VERSION-fpm.sock;
  292.         fastcgi_index index.php;
  293.         include fastcgi_params;
  294.     }
  295.  
  296.     location ~ /\.ht {
  297.         deny all;
  298.     }
  299. }
  300. EOF
  301.  
  302. # Restart Nginx & PHP-FPM Services
  303.  
  304. #service nginx restart
  305. service nginx reload
  306.  
  307. if [ ! -z "\$(ps aux | grep php-fpm | grep -v grep)" ]
  308. then
  309.     service php7.3-fpm restart > /dev/null 2>&1
  310.     service php7.2-fpm restart > /dev/null 2>&1
  311.     service php7.1-fpm restart > /dev/null 2>&1
  312.     service php7.0-fpm restart > /dev/null 2>&1
  313.     service php5.6-fpm restart > /dev/null 2>&1
  314.     service php5-fpm restart > /dev/null 2>&1
  315. fi
  316. # Add Forge User To www-data Group
  317.  
  318. usermod -a -G www-data $USER
  319. id $USER
  320. groups $USER
  321.  
  322. # Install Node.js
  323.  
  324. curl --silent --location https://deb.nodesource.com/setup_8.x | bash -
  325.  
  326. apt-get update
  327.  
  328. sudo apt-get install -y --force-yes nodejs
  329.  
  330. npm install -g pm2
  331. npm install -g gulp
  332. npm install -g yarn
  333.  
  334. # Set The Automated Root Password
  335.  
  336. export DEBIAN_FRONTEND=noninteractive
  337.  
  338. debconf-set-selections <<< "mysql-community-server mysql-community-server/data-dir select ''"
  339. debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password $MYSQL_ROOT_PASSWORD"
  340. debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password $MYSQL_ROOT_PASSWORD"
  341.  
  342. # Install MySQL
  343.  
  344. apt-get install -y mysql-server
  345.  
  346. # Configure Password Expiration
  347.  
  348. echo "default_password_lifetime = 0" >> /etc/mysql/mysql.conf.d/mysqld.cnf
  349.  
  350. # Configure Access Permissions For Root & Forge Users
  351.  
  352. sed -i '/^bind-address/s/bind-address.*=.*/bind-address = */' /etc/mysql/mysql.conf.d/mysqld.cnf
  353. mysql --user="root" --password="$MYSQL_ROOT_PASSWORD" -e "GRANT ALL ON *.* TO root@'$SERVER_IP' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';"
  354. mysql --user="root" --password="$MYSQL_ROOT_PASSWORD" -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';"
  355. service mysql restart
  356.  
  357. mysql --user="root" --password="$MYSQL_ROOT_PASSWORD" -e "CREATE USER '$USER'@'$SERVER_IP' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';"
  358. mysql --user="root" --password="$MYSQL_ROOT_PASSWORD" -e "GRANT ALL ON *.* TO '$USER'@'$SERVER_IP' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION;"
  359. mysql --user="root" --password="$MYSQL_ROOT_PASSWORD" -e "GRANT ALL ON *.* TO '$USER'@'%' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION;"
  360. mysql --user="root" --password="$MYSQL_ROOT_PASSWORD" -e "FLUSH PRIVILEGES;"
  361.  
  362. # Install & Configure Redis Server
  363.  
  364. apt-get install -y redis-server
  365. sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /etc/redis/redis.conf
  366. service redis-server restart
  367. systemctl enable redis-server
  368. # Install & Configure Memcached
  369.  
  370. apt-get install -y memcached
  371. sed -i 's/-l 127.0.0.1/-l 0.0.0.0/' /etc/memcached.conf
  372. service memcached restart
  373.  
  374. # Install & Configure Beanstalk
  375.  
  376. apt-get install -y --force-yes beanstalkd
  377. sed -i "s/BEANSTALKD_LISTEN_ADDR.*/BEANSTALKD_LISTEN_ADDR=0.0.0.0/" /etc/default/beanstalkd
  378.  
  379. if grep START= /etc/default/beanstalkd; then
  380.     sed -i "s/#START=yes/START=yes/" /etc/default/beanstalkd
  381. else
  382.     echo "START=yes" >> /etc/default/beanstalkd
  383. fi
  384.  
  385. service beanstalkd start
  386. sleep 5
  387. service beanstalkd restart
  388.  
  389. systemctl enable beanstalkd
  390.  
  391. # Configure Supervisor Autostart
  392.  
  393. systemctl enable supervisor.service
  394. service supervisor start
  395.  
  396. # Configure Swap Disk
  397.  
  398. if [ -f /swapfile ]; then
  399.     echo "Swap exists."
  400. else
  401.     fallocate -l $SWAP_SIZE /swapfile
  402.     chmod 600 /swapfile
  403.     mkswap /swapfile
  404.     swapon /swapfile
  405.     echo "/swapfile none swap sw 0 0" >> /etc/fstab
  406.     echo "vm.swappiness=30" >> /etc/sysctl.conf
  407.     echo "vm.vfs_cache_pressure=50" >> /etc/sysctl.conf
  408. fi
  409.  
  410. # Setup Unattended Security Upgrades
  411.  
  412. cat > /etc/apt/apt.conf.d/50unattended-upgrades << EOF
  413. Unattended-Upgrade::Allowed-Origins {
  414.     "Ubuntu bionic-security";
  415. };
  416. Unattended-Upgrade::Package-Blacklist {
  417.     //
  418. };
  419. EOF
  420.  
  421. cat > /etc/apt/apt.conf.d/10periodic << EOF
  422. APT::Periodic::Update-Package-Lists "1";
  423. APT::Periodic::Download-Upgradeable-Packages "1";
  424. APT::Periodic::AutocleanInterval "7";
  425. APT::Periodic::Unattended-Upgrade "1";
  426. EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement