-
Notifications
You must be signed in to change notification settings - Fork 24
/
install.sh
288 lines (229 loc) · 9.03 KB
/
install.sh
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
# Crucial paths
HOMEBREW_PATH='/opt/homebrew'
PHP_VERSION='8.3'
# Helper variables
TXTBOLD=$(tput bold)
BOLDGREEN=${TXTBOLD}$(tput setaf 2)
YELLOW=$(tput setaf 3)
GREEN=$(tput setaf 2)
TXTRESET=$(tput sgr0)
echo "${YELLOW}Getting dependencies...${TXTRESET}"
# Attempt to install Xcode Command Line Tools
xcode-select --install
# Get Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Inspect Homebrew installation
brew doctor
# Update Homebrew
brew update && brew upgrade
echo "${BOLDGREEN}Dependencies installed and up to date.${TXTRESET}"
# Install the latest nginx version
echo "${YELLOW}Installing nginx.${TXTRESET}"
brew tap homebrew/nginx
brew install nginx
sudo brew services start nginx
curl -IL http://127.0.0.1:80
echo "${BOLDGREEN}nginx installed and running.${TXTRESET}"
# Set up nginx and paths
echo "${YELLOW}Setting up nginx...${TXTRESET}"
# Chec if /etc/nginx exists
if [ -d "/etc/nginx" ]; then
echo "${YELLOW}Directory /etc/nginx exists.${TXTRESET}"
else
echo "${YELLOW}Directory /etc/nginx does not exist. Symlinking homebrew installation to it.${TXTRESET}"
sudo ln -sfnv "${HOMEBREW_PATH}/etc/nginx" "/etc/nginx"
# Creating the necessary directories
sudo mkdir -p "${HOMEBREW_PATH}/etc/nginx/global"
sudo mkdir -p "${HOMEBREW_PATH}/etc/nginx/sites-enabled"
sudo mkdir -p "${HOMEBREW_PATH}/etc/nginx/sites-available"
sudo mkdir -p "${HOMEBREW_PATH}/etc/nginx/global"
# Setting permissions
sudo chmod -R 775 "${HOMEBREW_PATH}/etc/nginx/global"
sudo chmod -R 775 "${HOMEBREW_PATH}/etc/nginx/sites-enabled"
sudo chmod -R 775 "${HOMEBREW_PATH}/etc/nginx/sites-available"
sudo chmod -R 775 "${HOMEBREW_PATH}/etc/nginx/global"
fi
echo "${YELLOW}Creating default nginx configurations...${TXTRESET}"
# Create default nginx configuration
sudo echo "user $(whoami) staff;
worker_processes 18;
events {
multi_accept on;
accept_mutex on;
worker_connections 1024;
}
http {
##
# Optimization
##
sendfile on;
sendfile_max_chunk 512k;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
keepalive_requests 100000;
types_hash_max_size 2048;
server_tokens off;
client_body_buffer_size 128k;
client_max_body_size 10m;
client_header_buffer_size 1k;
large_client_header_buffers 4 32k;
output_buffers 1 32k;
postpone_output 1460;
server_names_hash_max_size 1024;
#server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include ${HOMEBREW_PATH}/etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log off;
access_log /var/log/nginx/access.log combined;
error_log /var/log/nginx/error.log;
##
# Virtual Host Configs
##
include ${HOMEBREW_PATH}/etc/nginx/sites-enabled/*;
}" > "${HOMEBREW_PATH}/etc/nginx/nginx.conf"
echo "${YELLOW}Creating locations for logs...${TXTRESET}"
# Check if nginx log dir exists
if [ -d "/var/log/nginx" ]; then
echo "${YELLOW}Directory /var/log/nginx exists.${TXTRESET}"
else
echo "${YELLOW}Directory /var/log/nginx does not exist. Creating it and the access.log and error.log files.${TXTRESET}"
sudo mkdir -p /var/log/nginx
sudo touch /var/log/nginx/access.log
sudo chmod 777 /var/log/nginx/access.log
sudo touch /var/log/nginx/error.log
sudo chmod 777 /var/log/nginx/error.log
fi
echo "${YELLOW}Creating default PHP/nginx configurations...${TXTRESET}"
# Create default php configuration
sudo echo "location ~ \.php\$ {
proxy_intercept_errors on;
try_files \$uri /index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
include fastcgi_params;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}" > "${HOMEBREW_PATH}/etc/nginx/php.conf"
echo "${YELLOW}Creating default WordPress-related nginx configurations...${TXTRESET}"
# Create default WordPress-related nginx configuration
sudo echo "# WordPress single site rules.
# Designed to be included in any server {} block.
# Upstream to abstract backend connection(s) for php
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?\$args" part so non-default permalinks doesn't break when using query string
try_files \$uri \$uri/ /index.php?\$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin\$ \$scheme://\$host\$uri/ permanent;
# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)\$ {
access_log off;
log_not_found off;
expires max;
}" > "${HOMEBREW_PATH}/etc/nginx/global/wordpress.conf"
echo "${YELLOW}Creating default site vhost...${TXTRESET}"
# Create default site configuration
sudo echo "server {
listen 80 default_server;
root /var/www;
index index.html index.htm index.php;
server_name localhost;
include php.conf;
include global/wordpress.conf;
}" > "${HOMEBREW_PATH}/etc/nginx/sites-available/default"
echo "${YELLOW}Symlinking the default vhost...${TXTRESET}"
# Symlink the default site configuration
sudo ln -sfnv ${HOMEBREW_PATH}/etc/nginx/sites-available/default ${HOMEBREW_PATH}/etc/nginx/sites-enabled/default
echo "${YELLOW}Making sure /var/www exists and is linked to project directory...${TXTRESET}"
# Check if /var/www exists
if [ -d "/var/www" ]; then
echo "${YELLOW}Directory /var/www exists.${TXTRESET}"
else
echo "${YELLOW}Directory /var/www does not exist. Symlinking $HOME/Projects to it.${TXTRESET}"
sudo ln -sfnv $HOME/Projects /var/www
fi
echo "${YELLOW}Copying default nginx index to the project dir...${TXTRESET}"
# Figure out the nginx correct installation path
NGINX_VERSION=$(nginx -v 2>&1 | grep -o '[0-9.]*')
NGINX_HOMEBREW_PATH=$(find $HOMEBREW_PATH -name "nginx" -type d -maxdepth 3)
NGINX_HOMEBREW_VERSION_PATH=$(find $NGINX_HOMEBREW_PATH -name $NGINX_VERSION -type d -maxdepth 1)
# Copy the default index.html to the nginx root
sudo cp "${NGINX_HOMEBREW_VERSION_PATH}/html/index.html /var/www/"
echo "${YELLOW}Setting permissions...${TXTRESET}"
# Set the correct permissions
sudo chown -R $(whoami):staff /var/www
# Install PHP
echo "${YELLOW}Installing PHP...${TXTRESET}"
# This part is the same than in here:
# Install and use PHP $PHP_VERSION in your local macos-lemp-setup environment
# https://app.gitbook.com/o/PedExJWZmbCiZe4gDwKC/s/VVikkYgIZ9miBzwYDCYh/how-tos/install-and-use-php-8.3-in-your-local-macos-lemp-setup-environment
# Add the PHP and PHP Extension taps
brew tap shivammathur/php
brew tap shivammathur/extensions
# Install the PHP we need
brew install shivammathur/php/php@${PHP_VERSION}
# Link PHP CLI executable as `php`
brew link --overwrite --force shivammathur/php/php@${PHP_VERSION}
# Test installation, should display PHP 8.3.x (cli)
php -v
# Test php-fpm
lsof -Pni4 | grep LISTEN | grep php
# Symlink the PHP configuration
sudo ln -s $HOMEBREW_PATH/etc/php /etc/php
# Add PHP path to the PATH
sudo echo "export PATH=\"\$(brew --prefix php@${PHP_VERSION})/bin:\$PATH\"" >> ~/.bash_profile
# Restart
sudo brew services stop php@${PHP_VERSION}
sudo brew services start php@${PHP_VERSION}
echo "${BOLDGREEN}PHP installed and running.${TXTRESET}"
# Install MariaDB
echo "${YELLOW}Installing MariaDB...${TXTRESET}"
brew install mariadb
brew services start mariadb
echo "${BOLDGREEN}MariaDB installed and running.${TXTRESET}"
# Install MailHog
echo "${YELLOW}Installing MailHog...${TXTRESET}"
brew update && brew install mailhog
echo "${BOLDGREEN}MailHog installed (run mailhog to start mail server).${TXTRESET}"
# Install DNSmasq
echo "${YELLOW}Installing DNSmasq...${TXTRESET}"
brew install dnsmasq
curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/550c84393c4c1eef8a3e68bb720df561b5d3f175/dnsmasq.conf -o /usr/local/etc/dnsmasq.conf
sudo curl -L https://gist.githubusercontent.com/dtomasi/ab76d14338db82ec24a1fc137caff75b/raw/550c84393c4c1eef8a3e68bb720df561b5d3f175/dev -o /etc/resolver/dev
sudo brew services stop dnsmasq
sudo brew services start dnsmasq
echo "${BOLDGREEN}DNSmasq installed and configured.${TXTRESET}"
# Restart all services
echo "${YELLOW}Restarting services....${TXTRESET}"
# These need to be running as root, because of the port 80 and other privileges.
sudo brew services stop dnsmasq
sudo brew services start dnsmasq
sudo brew services stop nginx
sudo brew services start nginx
sudo brew services stop php@${PHP_VERSION}
sudo brew services start php@${PHP_VERSION}
brew services stop mariadb
brew services start mariadb
sudo brew services list
# All done
echo "${BOLDGREEN}You should now be able to use http://localhost. If not, test with commands sudo nginx -t and sudo php-fpm -t and fix errors. Add new vhosts to /opt/homebrew/etc/nginx/sites-available and symlink them just like you would do in production. Have fun!${TXTRESET}"