-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlamp.sh
More file actions
320 lines (266 loc) · 9.1 KB
/
lamp.sh
File metadata and controls
320 lines (266 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
# Function to display error messages
error_exit() {
echo "$1" >&2
exit 1
}
# Function to prompt for user confirmation
confirm() {
read -r -p "${1:-Are you sure?} [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
# Function to select PHP version
select_php_version() {
echo "Available PHP versions:"
echo "1) PHP 7.4"
echo "2) PHP 8.0"
echo "3) PHP 8.1"
echo "4) PHP 8.2"
echo "5) PHP 8.3"
echo "6) PHP 8.4"
read -p "Select PHP version (1-6): " php_choice
case $php_choice in
1) PHP_VERSION="7.4" ;;
2) PHP_VERSION="8.0" ;;
3) PHP_VERSION="8.1" ;;
4) PHP_VERSION="8.2" ;;
5) PHP_VERSION="8.3" ;;
6) PHP_VERSION="8.4" ;;
*) error_exit "Invalid PHP version selected" ;;
esac
echo "Selected PHP version: $PHP_VERSION"
}
# Function to prompt for MySQL credentials
get_mysql_credentials() {
read -p "Enter MySQL username (default: admin): " MYSQL_USER
MYSQL_USER=${MYSQL_USER:-admin}
read -s -p "Enter MySQL password: " MYSQL_PASS
echo
read -p "Enter database name (default: mydatabase): " DB_NAME
DB_NAME=${DB_NAME:-mydatabase}
}
# Update system
echo "Updating system packages..."
sudo apt update || error_exit "Failed to update system packages"
# Select web server
echo "Available web servers:"
echo "1) Apache (default)"
echo "2) Nginx"
read -p "Select web server (1-2, default: 1): " web_server_choice
web_server_choice=${web_server_choice:-1}
case $web_server_choice in
1)
echo "Installing Apache2..."
sudo apt install -y apache2 || error_exit "Failed to install Apache2"
;;
2)
echo "Installing Nginx..."
sudo apt install -y nginx || error_exit "Failed to install Nginx"
;;
*)
error_exit "Invalid web server selected"
;;
esac
# Add PHP repository and install PHP
echo "Setting up PHP repository..."
sudo apt install -y ca-certificates apt-transport-https software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
# Select PHP version
select_php_version
# Install PHP and extensions
echo "Installing PHP ${PHP_VERSION} and extensions..."
sudo apt install -y php${PHP_VERSION} \
php${PHP_VERSION}-common \
php${PHP_VERSION}-opcache \
php${PHP_VERSION}-cli \
php${PHP_VERSION}-gd \
php${PHP_VERSION}-curl \
php${PHP_VERSION}-mysql \
php${PHP_VERSION}-mbstring \
php${PHP_VERSION}-zip \
php${PHP_VERSION}-xml \
php${PHP_VERSION}-intl \
php${PHP_VERSION}-bcmath \
php${PHP_VERSION}-soap \
php${PHP_VERSION}-fpm \
php${PHP_VERSION}-imagick \
php${PHP_VERSION}-ldap || error_exit "Failed to install PHP"
# Install MySQL
echo "Installing MySQL server..."
sudo apt install -y mysql-server || error_exit "Failed to install MySQL"
# Get MySQL credentials
get_mysql_credentials
# Configure MySQL
echo "Configuring MySQL..."
sudo mysql -e "CREATE USER '${MYSQL_USER}'@'localhost' IDENTIFIED BY '${MYSQL_PASS}';"
sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'localhost' WITH GRANT OPTION;"
sudo mysql -e "FLUSH PRIVILEGES;"
sudo mysql -e "CREATE DATABASE ${DB_NAME};"
# Install Git
echo "Installing Git..."
sudo apt install -y git || error_exit "Failed to install Git"
# Install Composer
echo "Installing Composer..."
cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
HASH=$(curl -sS https://composer.github.io/installer.sig)
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
# Function to select Node.js version
select_node_version() {
echo "Available Node.js versions:"
echo "1) Node.js 16.x (LTS)"
echo "2) Node.js 18.x (LTS)"
echo "3) Node.js 20.x (Current)"
read -p "Select Node.js version (1-3): " node_choice
case $node_choice in
1) NODE_VERSION="16" ;;
2) NODE_VERSION="18" ;;
3) NODE_VERSION="20" ;;
*) error_exit "Invalid Node.js version selected" ;;
esac
echo "Selected Node.js version: $NODE_VERSION"
}
# Install Node.js (optional)
if confirm "Do you want to install Node.js?"; then
echo "Installing Node.js..."
select_node_version
cd ~
curl -sL https://deb.nodesource.com/setup_${NODE_VERSION}.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejs
fi
# Install Yarn and PM2 (optional, only if Node.js is installed)
if [ -x "$(command -v node)" ]; then
if confirm "Do you want to install Yarn?"; then
echo "Installing Yarn..."
sudo npm install --global yarn
fi
if confirm "Do you want to install PM2?"; then
echo "Installing PM2..."
sudo npm install --global pm2
fi
fi
# Get project details
read -p "Enter GitHub repository URL: " REPO_URL
# Navigate to web root
cd /var/www/html
# Clone the repository
echo "Cloning repository..."
sudo git clone "$REPO_URL" || error_exit "Failed to clone repository"
# Get the repository name from URL and cd into it
REPO_NAME=$(basename "$REPO_URL" .git)
cd "$REPO_NAME"
# Set proper permissions
sudo chown -R www-data:www-data /var/www/html/"$REPO_NAME"
sudo chmod -R 755 /var/www/html/"$REPO_NAME"
# Install dependencies with Composer
echo "Installing Composer dependencies..."
sudo -u www-data composer install || error_exit "Failed to install Composer dependencies"
# Setup environment file
echo "Setting up environment file..."
sudo cp .env.example .env || error_exit "Failed to create .env file"
# Generate application key
sudo php artisan key:generate || error_exit "Failed to generate application key"
# Update database credentials in .env
sudo sed -i "s/DB_DATABASE=.*/DB_DATABASE=${DB_NAME}/" .env
sudo sed -i "s/DB_USERNAME=.*/DB_USERNAME=${MYSQL_USER}/" .env
sudo sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=${MYSQL_PASS}/" .env
# Get domain name
read -p "Enter your domain name (e.g., example.com): " DOMAIN_NAME
# Create web server configuration
if [ "$web_server_choice" = "1" ]; then
# Apache configuration
echo "Creating Apache configuration..."
sudo tee /etc/apache2/sites-available/${DOMAIN_NAME}.conf << EOF
<VirtualHost *:80>
ServerAdmin admin@${DOMAIN_NAME}
ServerName ${DOMAIN_NAME}
DocumentRoot /var/www/html/${REPO_NAME}/public
<Directory /var/www/html/${REPO_NAME}/public>
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
# Enable Apache rewrite module
sudo a2enmod rewrite
# Enable the new site
sudo a2ensite ${DOMAIN_NAME}.conf
# Disable default site
sudo a2dissite 000-default.conf
# Restart Apache
sudo systemctl restart apache2
else
# Nginx configuration
echo "Creating Nginx configuration..."
sudo tee /etc/nginx/sites-available/${DOMAIN_NAME} << EOF
server {
listen 80;
listen [::]:80;
server_name ${DOMAIN_NAME};
root /var/www/html/${REPO_NAME}/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
EOF
# Create symbolic link
sudo ln -s /etc/nginx/sites-available/${DOMAIN_NAME} /etc/nginx/sites-enabled/
# Remove default nginx site
sudo rm -f /etc/nginx/sites-enabled/default
# Test nginx configuration
sudo nginx -t
# Restart Nginx
sudo systemctl restart nginx
fi
# SSL Installation (optional)
if confirm "Do you want to install SSL certificate?"; then
echo "WARNING: Before proceeding, ensure your domain's DNS A record points to this server's IP address."
if confirm "Have you configured the DNS settings?"; then
echo "Installing Certbot..."
if [ "$web_server_choice" = "1" ]; then
sudo apt install -y certbot python3-certbot-apache
echo "Generating SSL certificate..."
sudo certbot --apache
else
sudo apt install -y certbot python3-certbot-nginx
echo "Generating SSL certificate..."
sudo certbot --nginx
fi
else
echo "Please configure DNS settings first and run SSL installation later."
fi
fi
echo "Installation complete!"
echo "PHP Version: ${PHP_VERSION}"
echo "Node.js Version: ${NODE_VERSION}"
echo "MySQL User: ${MYSQL_USER}"
echo "Database Name: ${DB_NAME}"