|
1 | 1 | #!/bin/bash |
2 | | -VHOST_DIR='/etc/nginx/sites-available' |
3 | | -USER_DIR='/ebs/www' |
4 | | -USERNAME='' |
5 | | -USERPASS='' |
| 2 | +USER_DIR='/Users/ruscon' |
6 | 3 | DOMAIN='' |
7 | | -MSYQLPASS='' |
8 | 4 | USER_TRUNC='' |
9 | 5 | DOMAIN_TRUNC='' |
10 | 6 | DB_PASS='' |
11 | 7 |
|
12 | 8 | RET='' |
13 | 9 | function sanity_check { |
14 | | - if [ "$(id -u)" != "0" ]; then |
15 | | - echo "Script must be run as root." |
16 | | - exit 1 |
17 | | - fi |
18 | | - if [[ $1 != 2 ]]; then |
19 | | - echo $1 |
20 | | - echo "Usage: nginx-vhost.sh username example.com" |
21 | | - exit 4 |
| 10 | + if [ "$(id -u)" != "0" ]; then |
| 11 | + echo "Script must be run as root." |
| 12 | + exit 1 |
22 | 13 | fi |
23 | 14 |
|
24 | | - egrep "^$USERNAME:" /etc/passwd >/dev/null |
25 | | - if [ $? -eq 0 ]; then |
26 | | - echo "$USERNAME exists!" |
27 | | - exit 3 |
28 | | - fi |
29 | | - if [ ! -e /root/mysql ]; then |
30 | | - echo "Root MySQL password file missing. Please run create the file /root/mysql," |
31 | | - echo "And put your root password as the first and only line." |
32 | | - exit 4 |
33 | | - fi |
34 | | -} |
35 | | - |
36 | | -function password { |
37 | | - RET=$(cat /dev/urandom | tr -cd [:alnum:] | head -c ${1:-16}) |
38 | | -} |
39 | | - |
40 | | -function setup_user { |
41 | | - useradd -m -d $USER_DIR/$USERNAME -U $USERNAME |
42 | | - |
43 | | - password |
44 | | - PASSWORD=$RET |
45 | | - echo "$USERNAME:$PASSWORD" | chpasswd |
46 | | - |
47 | | - mkdir -p $USER_DIR/$USERNAME/$DOMAIN/htdocs |
48 | | - mkdir $USER_DIR/$USERNAME/$DOMAIN/logs |
49 | | - touch $USER_DIR/$USERNAME/$DOMAIN/logs/access.log |
50 | | - touch $USER_DIR/$USERNAME/$DOMAIN/logs/error.log |
51 | | - chown -R $USERNAME $USER_DIR/$USERNAME |
52 | | - chgrp -R $USERNAME $USER_DIR/$USERNAME |
53 | | - |
54 | | - echo "Create user: $USERNAME with password: $PASSWORD" |
| 15 | +# egrep "^$USERNAME:" /etc/passwd >/dev/null |
| 16 | +# if [ $? -eq 0 ]; then |
| 17 | +# echo "$USERNAME exists!" |
| 18 | +# exit 3 |
| 19 | +# fi |
| 20 | + |
| 21 | +# if [ ! -e /root/mysql ]; then |
| 22 | +# echo "Root MySQL password file missing. Please run create the file /root/mysql," |
| 23 | +# echo "And put your root password as the first and only line." |
| 24 | +# exit 4 |
| 25 | +# fi |
55 | 26 | } |
| 27 | +# |
| 28 | +#function password { |
| 29 | +# RET=$(cat /dev/urandom | tr -cd [:alnum:] | head -c ${1:-16}) |
| 30 | +#} |
| 31 | +# |
| 32 | +#function setup_user { |
| 33 | +# useradd -m -d $USER_DIR/$USERNAME -U $USERNAME |
| 34 | +# |
| 35 | +# password |
| 36 | +# PASSWORD=$RET |
| 37 | +# echo "$USERNAME:$PASSWORD" | chpasswd |
| 38 | +# |
| 39 | +# mkdir -p $USER_DIR/$USERNAME/$DOMAIN/htdocs |
| 40 | +# mkdir $USER_DIR/$USERNAME/$DOMAIN/logs |
| 41 | +# touch $USER_DIR/$USERNAME/$DOMAIN/logs/access.log |
| 42 | +# touch $USER_DIR/$USERNAME/$DOMAIN/logs/error.log |
| 43 | +# chown -R $USERNAME $USER_DIR/$USERNAME |
| 44 | +# chgrp -R $USERNAME $USER_DIR/$USERNAME |
| 45 | +# |
| 46 | +# echo "Create user: $USERNAME with password: $PASSWORD" |
| 47 | +#} |
56 | 48 | function php_pool { |
57 | | - if [ ! -f /etc/php5/fpm/port ]; then |
58 | | - echo "Cannot access /etc/php5/fpm/port" |
59 | | - exit 2 |
60 | | - fi |
61 | | - |
62 | | - # Grab the port from file, and increment. |
63 | | - PORT=$(cat /etc/php5/fpm/port) |
64 | | - echo $(($PORT + 1)) > /etc/php5/fpm/port |
65 | | - |
66 | | - cp /etc/php5/fpm/pool.template /etc/php5/fpm/pools/$DOMAIN |
67 | | - sed -i "s|example.com|$DOMAIN|" /etc/php5/fpm/pools/$DOMAIN |
68 | | - |
69 | | - sed -i "s|PORT|$PORT|" /etc/php5/fpm/pools/$DOMAIN |
70 | | - sed -i "s|user = example|user = $USERNAME|" /etc/php5/fpm/pools/$DOMAIN |
71 | | - sed -i "s|group = example|group = $USERNAME|" /etc/php5/fpm/pools/$DOMAIN |
| 49 | +# if [ ! -f /etc/php5/fpm/port ]; then |
| 50 | +# echo "Cannot access /etc/php5/fpm/port" |
| 51 | +# exit 2 |
| 52 | +# fi |
| 53 | +# |
| 54 | +# # Grab the port from file, and increment. |
| 55 | +# PORT=$(cat /etc/php5/fpm/port) |
| 56 | +# echo $(($PORT + 1)) > /etc/php5/fpm/port |
| 57 | + |
| 58 | + cp pool.template /usr/local/etc/php/5.4/fpm.d/$PROJECT.conf |
| 59 | + sed -i.tmp "s|example.com|$DOMAIN|g" /usr/local/etc/php/5.4/fpm.d/$PROJECT.conf |
| 60 | + rm /usr/local/etc/php/5.4/fpm.d/$PROJECT.conf.tmp |
| 61 | + |
| 62 | +# sed -i "s|PORT|$PORT|" /usr/local/etc/php/5.4/fpm.d/$DOMAIN |
| 63 | +# sed -i "s|user = example|user = $USERNAME|" /usr/local/etc/php/5.4/fpm.d/$DOMAIN |
| 64 | +# sed -i "s|group = example|group = $USERNAME|" /usr/local/etc/php/5.4/fpm.d/$DOMAIN |
72 | 65 | echo "Added $DOMAIN to the PHP pool" |
73 | 66 | } |
| 67 | + |
74 | 68 | function nginx_vhost { |
75 | | - cp /etc/nginx/vhost.template /etc/nginx/sites-available/$DOMAIN |
76 | | - sed -i "s|example.com|$DOMAIN|g" /etc/nginx/sites-available/$DOMAIN |
77 | | - sed -i "s|username|$USERNAME|g" /etc/nginx/sites-available/$DOMAIN |
78 | | - sed -i "s|PORT|$PORT|g" /etc/nginx/sites-available/$DOMAIN |
| 69 | + cp vhost.template /usr/local/etc/nginx/sites-available/$DOMAIN |
| 70 | + sed -i.tmp "s|example.com|$DOMAIN|g" /usr/local/etc/nginx/sites-available/$DOMAIN |
| 71 | + rm /usr/local/etc/nginx/sites-available/$DOMAIN.tmp |
| 72 | +# sed -i "s|username|$USERNAME|g" /usr/local/etc/nginx/sites-available/$DOMAIN |
| 73 | +# sed -i "s|PORT|$PORT|g" /usr/local/etc/nginx/sites-available/$DOMAIN |
79 | 74 | # Should probably sed through and replace /ebs/www with $USER_DIR |
80 | 75 |
|
81 | 76 | # Enable the site |
82 | | - ln -s /etc/nginx/sites-available/$DOMAIN /etc/nginx/sites-enabled/$DOMAIN |
83 | | - echo "Enabled $DOMAIN in the web server" |
84 | | -} |
85 | | -function server_reload { |
86 | | - /etc/init.d/php5-fpm reload |
87 | | - /etc/init.d/nginx reload |
88 | | - echo "Servers reloaded" |
89 | | -} |
90 | | -function prepare_mysql { |
91 | | - # Generate dbuser password |
92 | | - password |
93 | | - DB_PASS=$RET |
| 77 | + ln -s /usr/local/etc/nginx/sites-available/$DOMAIN /usr/local/etc/nginx/sites-enabled/$DOMAIN |
94 | 78 |
|
95 | | - # Truncate username (15 chars max) and dbname (63 chars max) |
96 | | - USER_TRUNC=$(echo $USERNAME | cut -c1-15) |
| 79 | + mkdir "$USER_DIR/projects/$PROJECT" |
| 80 | + chown ruscon:staff "$USER_DIR/projects/$PROJECT" |
| 81 | + ln -s $USER_DIR/projects/$PROJECT /usr/local/var/www/$DOMAIN |
97 | 82 |
|
98 | | - # This should be a separate user with only create perms. |
99 | | - echo "CREATE DATABASE $USER_TRUNC; |
100 | | -GRANT ALL PRIVILEGES ON $USER_TRUNC.* to '$USER_TRUNC'@'localhost' IDENTIFIED BY '$DB_PASS';" > $USERNAME.sql |
101 | | - mysql -u root -p$(cat /root/mysql) < $USERNAME.sql |
102 | | - rm $USERNAME.sql |
103 | | - echo "Created MySQL user: $USER_TRUNC password: $DB_PASS database: $DOMAIN_TRUNC" |
104 | | -} |
105 | | -function add_to_ftp { |
106 | | - usermod -g proftpd $USERNAME |
| 83 | + echo "Enabled $DOMAIN in the web server" |
107 | 84 | } |
108 | | -function install_wordpress { |
109 | | - wget http://wordpress.org/latest.tar.gz |
110 | | - tar xf latest.tar.gz |
111 | | - mv wordpress/* $USER_DIR/$USERNAME/$DOMAIN/htdocs/ |
112 | | - cp $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config-sample.php $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
113 | | - sed -i "s|database_name_here|$USER_TRUNC|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
114 | | - sed -i "s|username_here|$USERNAME|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
115 | | - sed -i "s|password_here|$DB_PASS|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
116 | | - |
117 | | - echo " define(\"FTP_HOST\", \"$DOMAIN\"); |
118 | | - define(\"FTP_USER\", \"$USERNAME\"); |
119 | | - define(\"FTP_PASS\", \"$USERPASS\");" >> $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
120 | | - |
121 | | - # Add in salts from the wordpress salt generator |
122 | | - wget https://api.wordpress.org/secret-key/1.1/salt/ |
123 | | - sed -i "s/|/a/g" index.html |
124 | | -# cat index.html | while read line; do |
125 | | - # sed -i "s|define('.*',.*'put your unique phrase here');|$line|" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
126 | | -# sed 1d $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
127 | | -# done |
128 | | - sed -i '/#@-/r index.html' $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
129 | | - sed -i "/#@+/,/#@-/d" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
130 | | - rm index.html |
131 | | - rm latest.tar.gz |
132 | 85 |
|
133 | | - # Add in FTP stuff, even though that's not defined yet.. |
134 | | - |
135 | | - # Own these new files |
136 | | - chown -R $USERNAME $USER_DIR/$USERNAME/$DOMAIN/htdocs/* |
137 | | - chgrp -R $USERNAME $USER_DIR/$USERNAME/$DOMAIN/htdocs/* |
138 | | - # Make sure no one else can read this file |
139 | | - chmod 700 $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
140 | | - |
141 | | - #echo "Wordpress for $DOMAIN installed." |
142 | | - #echo "Visit http://$DOMAIN/wp-admin/install.php to complete installation" |
| 86 | +function ect_host { |
| 87 | + echo "127.0.0.1 $DOMAIN" >> /etc/hosts |
143 | 88 | } |
144 | 89 |
|
| 90 | +#function server_reload { |
| 91 | +# /etc/init.d/php5-fpm reload |
| 92 | +# /etc/init.d/nginx reload |
| 93 | +# echo "Servers reloaded" |
| 94 | +#} |
| 95 | +#function prepare_mysql { |
| 96 | +# # Generate dbuser password |
| 97 | +# password |
| 98 | +# DB_PASS=$RET |
| 99 | +# |
| 100 | +# # Truncate username (15 chars max) and dbname (63 chars max) |
| 101 | +# USER_TRUNC=$(echo $USERNAME | cut -c1-15) |
| 102 | +# |
| 103 | +# # This should be a separate user with only create perms. |
| 104 | +# echo "CREATE DATABASE $USER_TRUNC; |
| 105 | +#GRANT ALL PRIVILEGES ON $USER_TRUNC.* to '$USER_TRUNC'@'localhost' IDENTIFIED BY '$DB_PASS';" > $USERNAME.sql |
| 106 | +# mysql -u root -p$(cat /root/mysql) < $USERNAME.sql |
| 107 | +# rm $USERNAME.sql |
| 108 | +# echo "Created MySQL user: $USER_TRUNC password: $DB_PASS database: $DOMAIN_TRUNC" |
| 109 | +#} |
| 110 | + |
| 111 | +#function add_to_ftp { |
| 112 | +# usermod -g proftpd $USERNAME |
| 113 | +#} |
| 114 | + |
| 115 | +#function install_wordpress { |
| 116 | +# wget http://wordpress.org/latest.tar.gz |
| 117 | +# tar xf latest.tar.gz |
| 118 | +# mv wordpress/* $USER_DIR/$USERNAME/$DOMAIN/htdocs/ |
| 119 | +# cp $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config-sample.php $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 120 | +# sed -i "s|database_name_here|$USER_TRUNC|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 121 | +# sed -i "s|username_here|$USERNAME|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 122 | +# sed -i "s|password_here|$DB_PASS|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 123 | +# |
| 124 | +# echo " define(\"FTP_HOST\", \"$DOMAIN\"); |
| 125 | +# define(\"FTP_USER\", \"$USERNAME\"); |
| 126 | +# define(\"FTP_PASS\", \"$USERPASS\");" >> $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 127 | +# |
| 128 | +# # Add in salts from the wordpress salt generator |
| 129 | +# wget https://api.wordpress.org/secret-key/1.1/salt/ |
| 130 | +# sed -i "s/|/a/g" index.html |
| 131 | +## cat index.html | while read line; do |
| 132 | +## sed -i "s|define('.*',.*'put your unique phrase here');|$line|" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 133 | +## sed 1d $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 134 | +## done |
| 135 | +# sed -i '/#@-/r index.html' $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 136 | +# sed -i "/#@+/,/#@-/d" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 137 | +# rm index.html |
| 138 | +# rm latest.tar.gz |
| 139 | +# |
| 140 | +# # Add in FTP stuff, even though that's not defined yet.. |
| 141 | +# |
| 142 | +# # Own these new files |
| 143 | +# chown -R $USERNAME $USER_DIR/$USERNAME/$DOMAIN/htdocs/* |
| 144 | +# chgrp -R $USERNAME $USER_DIR/$USERNAME/$DOMAIN/htdocs/* |
| 145 | +# # Make sure no one else can read this file |
| 146 | +# chmod 700 $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php |
| 147 | +# |
| 148 | +# #echo "Wordpress for $DOMAIN installed." |
| 149 | +# #echo "Visit http://$DOMAIN/wp-admin/install.php to complete installation" |
| 150 | +#} |
| 151 | + |
145 | 152 | ############################################################################## |
146 | 153 | # Start of program |
147 | | -USERNAME=$1 |
148 | | -DOMAIN=$2 |
| 154 | +#USERNAME=$1 |
| 155 | +PROJECT=$1 |
| 156 | +DOMAIN="$1.dev" |
149 | 157 |
|
150 | 158 | sanity_check $# |
151 | | -setup_user |
| 159 | +#setup_user |
152 | 160 | php_pool |
153 | 161 | nginx_vhost |
154 | | -server_reload |
155 | | -prepare_mysql |
156 | | -add_to_ftp |
157 | | -install_wordpress |
| 162 | +ect_host |
| 163 | +#server_reload |
| 164 | +#prepare_mysql |
| 165 | +#add_to_ftp |
| 166 | +#install_wordpress |
158 | 167 | exit 0 |
0 commit comments