11#! /bin/bash
22set -e
33
4+ # Colors for output
45RED=' \033[0;31m'
56GREEN=' \033[0;32m'
67YELLOW=' \033[1;33m'
@@ -10,74 +11,11 @@ log_success() {
1011 echo -e " ${GREEN} [✓] $1 ${NC} "
1112}
1213
13- log_error () {
14- echo -e " ${RED} [✗] $1 ${NC} "
15- exit 1
16- }
17-
1814log_info () {
1915 echo -e " ${YELLOW} [i] $1 ${NC} "
2016}
2117
22- check_nginx () {
23- log_info " Checking Nginx process status..."
24- local max_attempts=5
25- local attempt=0
26-
27- while [ $attempt -lt $max_attempts ]; do
28- if pgrep nginx > /dev/null; then
29- log_success " Nginx started successfully"
30- return 0
31- fi
32-
33- log_info " Waiting for Nginx to start... (Attempt $(( attempt+ 1 )) /$max_attempts )"
34- sleep 3
35- attempt=$(( attempt+ 1 ))
36- done
37-
38- log_error " Failed to start Nginx after $max_attempts attempts"
39- }
40-
41- check_php_fpm () {
42- log_info " Checking PHP-FPM process status..."
43- local max_attempts=5
44- local attempt=0
45-
46- while [ $attempt -lt $max_attempts ]; do
47- if pgrep php-fpm > /dev/null; then
48- log_success " PHP-FPM started successfully"
49- return 0
50- fi
51-
52- log_info " Waiting for PHP-FPM to start... (Attempt $(( attempt+ 1 )) /$max_attempts )"
53- sleep 3
54- attempt=$(( attempt+ 1 ))
55- done
56-
57- log_error " Failed to start PHP-FPM after $max_attempts attempts"
58- }
59-
60- echo -e " \n${YELLOW} Lerama: Starting${NC} \n"
61-
62- # Set timezone
63- if [ -n " $TZ " ]; then
64- log_info " Setting timezone to $TZ ..."
65- ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
66- echo $TZ > /etc/timezone
67-
68- echo " date.timezone = $TZ " > /usr/local/etc/php/conf.d/timezone.ini
69- log_success " Timezone set to $TZ for both system and PHP"
70- else
71- log_info " No TZ environment variable set, using UTC as default timezone"
72-
73- ln -snf /usr/share/zoneinfo/UTC /etc/localtime
74- echo " UTC" > /etc/timezone
75-
76- echo " date.timezone = UTC" > /usr/local/etc/php/conf.d/timezone.ini
77- log_success " Timezone set to UTC for both system and PHP"
78- fi
79-
80- # Create or update .env file with environment variables
18+ # Create .env file with environment variables
8119log_info " Setting up environment variables in /app/.env..."
8220cat > /app/.env << EOL
8321APP_NAME=Lerama
10240
10341log_success " Environment variables set in /app/.env"
10442
105- log_info " Starting PHP-FPM..."
106- php-fpm &
107- check_php_fpm
108-
109- # Ensure /app/storage directory exists and has correct permissions
110- log_info " Checking /app/public/storage directory..."
111- if [ ! -d /app/public/storage ]; then
112- log_info " Creating /app/public/storage directory..."
113- mkdir -p /app/public/storage
114- log_success " /app/public/storage directory created"
115- fi
116-
117- # Database
118- log_info " Checking database tables..."
119- php /setup/check-db.php
120-
121- # NGINX
122- log_info " Checking Nginx configuration..."
123- nginx -t
124- if [ $? -ne 0 ]; then
125- log_error " Invalid Nginx configuration"
126- else
127- log_success " Valid Nginx configuration"
128- fi
129-
130- log_info " Starting Nginx..."
131- nginx -g " daemon off;" &
132- check_nginx
133-
134- # Cron
135- log_info " Setting up cron jobs..."
136-
137- # Create a directory for cron scripts
138- mkdir -p /app/cron-scripts
139- chmod 755 /app/cron-scripts
140-
141- cat > /app/cron-scripts/feed_process.sh << 'EOL '
142- #!/bin/bash
143- echo "$(date): Starting feed:process cron job"
144- /usr/local/bin/php /app/bin/lerama feed:process 2>&1 | sed "s/^/[feed:process] /"
145- echo "$(date): Finished feed:process cron job"
146- EOL
147-
148- cat > /app/cron-scripts/feed_check_status.sh << 'EOL '
149- #!/bin/bash
150- echo "$(date): Starting feed:check-status cron job"
151- /usr/local/bin/php /app/bin/lerama feed:check-status 2>&1 | sed "s/^/[feed:check-status] /"
152- echo "$(date): Finished feed:check-status cron job"
153- EOL
154-
155- cat > /app/cron-scripts/proxy_update.sh << 'EOL '
156- #!/bin/bash
157- echo "$(date): Starting proxy_update cron job"
158- /usr/local/bin/php /app/bin/lerama proxy:update 2>&1 | sed "s/^/[proxy:update] /"
159- echo "$(date): Finished proxy_update cron job"
160- EOL
161-
162- # Make the scripts executable
163- chmod +x /app/cron-scripts/* .sh
164-
165- # Set up crontab to use the wrapper scripts and redirect output to Docker logs
166- (
167- echo " 0 * * * * /app/cron-scripts/feed_process.sh >> /proc/1/fd/1 2>> /proc/1/fd/2"
168- echo " 30 * * * * /app/cron-scripts/feed_check_status.sh >> /proc/1/fd/1 2>> /proc/1/fd/2"
169- echo " 0 0 * * * /app/cron-scripts/proxy_update.sh >> /proc/1/fd/1 2>> /proc/1/fd/2"
170- ) | crontab -
171-
172- service cron restart
173-
174- log_success " Cron jobs added with stdout logging"
175-
176- # Update proxy list if PROXY_LIST is defined
43+ # Update proxy list if PROXY_LIST environment variable is defined
17744if [ -n " $PROXY_LIST " ]; then
178- log_info " PROXY_LIST environment variable detected, updating proxy list..."
179- php /app/bin/lerama proxy:update
180- if [ $? -eq 0 ]; then
181- log_success " Proxy list updated successfully"
182- else
183- log_info " Failed to update proxy list, will retry later"
184- fi
185- fi
186-
187- # Set correct permissions for /app/storage
188- log_info " Setting permissions for /app/public/storage..."
189- chown -R www-data:www-data /app/public/storage
190- chmod -R 755 /app/public/storage
191- log_success " Permissions set for /app/public/storage"
192-
193- # PHP-FPM
194- if [ ! -d /var/run/php ]; then
195- log_info " Creating PHP-FPM directory..."
196- mkdir -p /var/run/php
197- chown -R www-data:www-data /var/run/php
198- log_success " PHP-FPM directory created"
199- fi
200-
201- echo -e " \n${GREEN} Lerama: Initialized ===${NC} \n"
202-
203- wait -n
45+ log_info " PROXY_LIST environment variable detected, will update proxy list after startup..."
46+ fi
0 commit comments