SHARE
TWEET
Untitled
a guest
Jun 30th, 2013
58
Never
- #!/bin/bash
- ##
- # KamiNuvini - kami@nuvini.com
- # This script does the following:
- # 1. Create directory for new user. This directory will have a public_html, logs and tmp folder by default.
- # 2. Add user, SFTP Chroot them, disallow normal SSH access. Allow PasswordAuthentication for user.
- # 3. Add separate PHP-FPM Pool for user.
- # 4. Add nginx-vhost and enable it.
- # 5. Setting up directory permissions.
- # 6. Restarting Services
- # Note that SFTP chrooting must be set up prior to running this script. The SFTP Chroot condition is the group
- # filetransfer.
- ##
- ## Fetching Variables
- echo -n "Give domain name: "
- read DOMAIN
- echo -n "Give username: "
- read USER
- # Homedir location, please set at least 2 levels deep for proper chrooting.
- HOMEDIR=/srv/www/$DOMAIN
- ## 1. Creating directories
- mkdir -p $HOMEDIR/public_html
- mkdir -p $HOMEDIR/tmp
- mkdir -p $HOMEDIR/logs
- ## 2. Creating User
- groupadd $USER
- useradd -g $USER -G filetransfer -d $HOMEDIR/ -s /usr/sbin/nologin $USER
- passwd $USER
- ### 2.1 Allow PasswordAuthentication through SSH
- cat >> /etc/ssh/sshd_config << EOL
- Match User $USER
- PasswordAuthentication yes
- EOL
- ## 3. Adding PHP-FPM Pool
- cat > /etc/php5/fpm/pool.d/$DOMAIN.conf << EOL
- [$USER]
- user = $USER
- group = $USER
- listen = $HOMEDIR/tmp/php-fpm-$USER.sock
- listen.owner = $USER
- listen.group = $USER
- listen.mode = 0666
- pm = dynamic
- pm.max_children = 9
- pm.start_servers = 3
- pm.min_spare_servers = 2
- pm.max_spare_servers = 3
- pm.max_requests = 500
- chdir = /
- php_admin_value[open_basedir] = $HOMEDIR/public_html:$HOMEDIR/tmp:/usr/share/php5:/usr/share/php
- php_admin_value[disable_functions] = apache_child_terminate, apache_setenv, define_syslog_variables, escapeshellarg, escapeshellcmd, eval, exec, fp, fput, ftp_connect, ftp_exec, ftp_get, ftp_login, ftp_nb_fput, ftp_put, ftp_raw, ftp_rawlist, highlight_file, ini_alter, ini_get_all, ini_restore, inject_code, mysql_pconnect, openlog, passthru, php_uname, phpAds_remoteInfo, phpAds_XmlRpc, phpAds_xmlrpcDecode, phpAds_xmlrpcEncode, popen, posix_getpwuid, posix_kill, posix_mkfifo, posix_setpgid, posix_setsid, posix_setuid, posix_setuid, posix_uname, proc_close, proc_get_status, proc_nice, proc_open, proc_terminate, shell_exec, syslog, system, xmlrpc_entity_decode, symlink
- php_flag[expose_php] = off
- php_value[memory_limit] = 128M
- php_admin_value[upload_tmp_dir] = "$HOMEDIR/tmp/"
- EOL
- ## 4. Setting up nginx vhost
- cat > /etc/nginx/sites-available/$DOMAIN << EOL
- server
- {
- listen 80;
- listen [::]:80;
- server_name $DOMAIN www.$DOMAIN;
- client_max_body_size 20m;
- client_body_buffer_size 128k;
- access_log $HOMEDIR/logs/access.log;
- error_log $HOMEDIR/logs/error.log;
- root $HOMEDIR/public_html;
- add_header X-Frame-Options SAMEORIGIN;
- location /
- {
- index index.html index.htm index.php;
- # Enable gzip compression
- gzip_static on;
- }
- location ~ /\.
- {
- access_log off;
- log_not_found off;
- deny all;
- }
- ## Only allow these request methods ##
- if (\$request_method !~ ^(GET|HEAD|POST)$ )
- {
- return 444;
- }
- ## Do not accept DELETE, SEARCH and other methods ##
- # Pass all .php files onto a php-fpm/php-fcgi server.
- location ~ \.php$
- {
- # Important, don't remove for security reasons
- try_files \$uri =404;
- fastcgi_split_path_info ^(.+\.php)(/.+)$;
- include fastcgi_params;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $HOMEDIR/public_html\$fastcgi_script_name;
- fastcgi_pass unix:$HOMEDIR/tmp/php-fpm-$USER.sock;
- }
- }
- EOL
- ### 4.1 Enable the vhost
- ln -s /etc/nginx/sites-available/$DOMAIN /etc/nginx/sites-enabled/$DOMAIN
- ## 5. Setting permissions
- chown -R $USER:www-data $HOMEDIR/logs/
- chown -R $USER:www-data $HOMEDIR/public_html/
- chown -R $USER:www-data $HOMEDIR/tmp
- chmod 0710 $HOMEDIR/logs
- chmod 0710 $HOMEDIR/public_html/
- chmod 0710 $HOMEDIR/tmp/
- ## Restarting Services
- /etc/init.d/php5-fpm restart
- /etc/init.d/nginx restart
- /etc/init.d/ssh try-restart
RAW Paste Data
