|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Exit immediately if a command exits with a non-zero status |
| 4 | +set -e |
| 5 | + |
| 6 | +# Log file |
| 7 | +LOG_FILE="/var/www/privatemarket/script.log" |
| 8 | + |
| 9 | +# Redirect stdout and stderr to the log file |
| 10 | +exec > >(tee -a "$LOG_FILE") 2>&1 |
| 11 | + |
| 12 | +# User-defined variables |
| 13 | +SSUSER="marketuser" |
| 14 | +SSPASSWORD="marketpassword" |
| 15 | +DB_PASSWORD="newpassword" |
| 16 | +DB_NAME="marketplace" |
| 17 | +MARKETPLACE="/var/www/privatemarket" |
| 18 | + |
| 19 | +# Log function |
| 20 | +log() { |
| 21 | + echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" |
| 22 | +} |
| 23 | + |
| 24 | +# Function to add sudo user |
| 25 | +add_sudo_user() { |
| 26 | + log "Adding sudo user $SSUSER" |
| 27 | + adduser "$SSUSER" --disabled-password --gecos "" && \ |
| 28 | + echo "$SSUSER:$SSPASSWORD" | chpasswd |
| 29 | + adduser "$SSUSER" sudo |
| 30 | +} |
| 31 | + |
| 32 | +# Function to update and upgrade packages |
| 33 | +update_and_upgrade() { |
| 34 | + log "Updating and upgrading packages" |
| 35 | + apt-get -o Acquire::ForceIPv4=true update -y |
| 36 | + sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o DPkg::options::="--force-confdef" -o DPkg::options::="--force-confold" install grub-pc |
| 37 | + apt-get -o Acquire::ForceIPv4=true update -y |
| 38 | + apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -q -y |
| 39 | + apt-get install -y software-properties-common |
| 40 | +} |
| 41 | + |
| 42 | +# Function to install PHP and extensions |
| 43 | +install_php() { |
| 44 | + log "Installing PHP and extensions" |
| 45 | + add-apt-repository -y ppa:ondrej/php |
| 46 | + apt-get update |
| 47 | + apt-get install -y php7.2-fpm php-mysql php7.2-mbstring php7.2-xml php7.2-xmlrpc php7.2-gmp php7.2-curl php7.2-gd php7.2-zip unzip composer |
| 48 | + |
| 49 | + # Configure PHP |
| 50 | + sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.2/fpm/php.ini |
| 51 | + systemctl restart php7.2-fpm |
| 52 | +} |
| 53 | + |
| 54 | +# Function to install and configure Nginx |
| 55 | +install_nginx() { |
| 56 | + log "Installing and configuring Nginx" |
| 57 | + apt-get install -y nginx |
| 58 | + sudo ufw allow 'Nginx HTTP' |
| 59 | + |
| 60 | + # Create the log directory |
| 61 | + mkdir -p "$MARKETPLACE/logs" |
| 62 | + chown -R www-data:www-data "$MARKETPLACE/logs" |
| 63 | + |
| 64 | + # Create Nginx configuration for the website |
| 65 | + cat <<END >/etc/nginx/sites-available/default |
| 66 | +server { |
| 67 | + listen 80; |
| 68 | + listen [::]:80; |
| 69 | + root $MARKETPLACE/public; |
| 70 | + index index.php index.html index.htm index.nginx-debian.html; |
| 71 | + server_name domain.com; |
| 72 | +
|
| 73 | + location / { |
| 74 | + try_files \$uri \$uri/ /index.php?\$query_string; |
| 75 | + } |
| 76 | +
|
| 77 | + location ~ \.php\$ { |
| 78 | + include snippets/fastcgi-php.conf; |
| 79 | + fastcgi_pass unix:/run/php/php7.2-fpm.sock; |
| 80 | + } |
| 81 | +
|
| 82 | + error_log $MARKETPLACE/logs/error.log; |
| 83 | + access_log $MARKETPLACE/logs/access.log; |
| 84 | +} |
| 85 | +END |
| 86 | + ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/ |
| 87 | + |
| 88 | + log "Checking Nginx configuration" |
| 89 | + nginx -t |
| 90 | + |
| 91 | + log "Reloading Nginx" |
| 92 | + systemctl reload nginx |
| 93 | + |
| 94 | + log "Restarting Nginx" |
| 95 | + systemctl restart nginx |
| 96 | +} |
| 97 | + |
| 98 | +# Function to install MySQL Server |
| 99 | +install_mysql() { |
| 100 | + log "Installing MySQL Server" |
| 101 | + echo "mysql-server mysql-server/root_password password $DB_PASSWORD" | sudo debconf-set-selections |
| 102 | + echo "mysql-server mysql-server/root_password_again password $DB_PASSWORD" | sudo debconf-set-selections |
| 103 | + apt-get -y install mysql-server |
| 104 | + mysql -uroot -p"$DB_PASSWORD" -e "CREATE DATABASE $DB_NAME;" |
| 105 | + service mysql restart |
| 106 | +} |
| 107 | + |
| 108 | +# Function to install Elasticsearch |
| 109 | +install_elasticsearch() { |
| 110 | + log "Elasticsearch" |
| 111 | + # Install Elasticsearch |
| 112 | + wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.3.1/elasticsearch-2.3.1.deb |
| 113 | + sudo dpkg -i elasticsearch-2.3.1.deb |
| 114 | + sudo service elasticsearch start |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +# Function to install and configure Redis |
| 119 | +install_redis() { |
| 120 | + log "Installing and configuring Redis" |
| 121 | + apt-get install -y redis-server |
| 122 | + sed -i 's/supervised no/supervised systemd/' /etc/redis/redis.conf |
| 123 | + systemctl restart redis.service |
| 124 | +} |
| 125 | + |
| 126 | +# Function to install Node.js and NPM |
| 127 | +install_node() { |
| 128 | + log "Installing Node.js and NPM" |
| 129 | + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash |
| 130 | + export NVM_DIR="$HOME/.nvm" |
| 131 | + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" |
| 132 | + nvm install 18 |
| 133 | + nvm use 18 |
| 134 | +} |
| 135 | + |
| 136 | +# Function to set permissions for Marketplace files |
| 137 | +set_permissions() { |
| 138 | + log "Setting permissions for Marketplace files" |
| 139 | + chown -R www-data:www-data "$MARKETPLACE/public" |
| 140 | + chmod 755 /var/www |
| 141 | + chmod -R 755 "$MARKETPLACE/bootstrap/cache" |
| 142 | + chmod -R 755 "$MARKETPLACE/storage" |
| 143 | + sudo -u www-data php "$MARKETPLACE/artisan" storage:link |
| 144 | + mkdir -p "$MARKETPLACE/storage/public/products" |
| 145 | +} |
| 146 | + |
| 147 | +# Function to install Composer dependencies |
| 148 | +install_composer_dependencies() { |
| 149 | + log "Installing Composer dependencies" |
| 150 | + sudo -u "$SSUSER" -H sh -c "cd $MARKETPLACE && \ |
| 151 | + php -r \"copy('https://getcomposer.org/installer', 'composer-setup.php');\" && \ |
| 152 | + HASH=\$(wget -q -O - https://composer.github.io/installer.sig) && \ |
| 153 | + php -r \"if (hash_file('sha384', 'composer-setup.php') === '\$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;\" && \ |
| 154 | + php composer-setup.php && \ |
| 155 | + php -r \"unlink('composer-setup.php');\" && \ |
| 156 | + php composer.phar install" |
| 157 | +} |
| 158 | + |
| 159 | +# Function to install Node packages and build assets |
| 160 | +install_node_packages() { |
| 161 | + log "Installing Node packages and building assets" |
| 162 | + sudo -u "$SSUSER" -H sh -c "cd $MARKETPLACE && npm install && npm run prod" |
| 163 | +} |
| 164 | + |
| 165 | +# Function to set up environment configuration |
| 166 | +setup_environment() { |
| 167 | + log "Setting up environment configuration" |
| 168 | + cp "$MARKETPLACE/.env.example" "$MARKETPLACE/.env" |
| 169 | + php "$MARKETPLACE/artisan" key:generate |
| 170 | +} |
| 171 | + |
| 172 | +# Function to migrate the database |
| 173 | +migrate_database() { |
| 174 | + log "Migrating the database" |
| 175 | + php "$MARKETPLACE/artisan" migrate |
| 176 | +} |
| 177 | + |
| 178 | +# Main script execution |
| 179 | +main() { |
| 180 | + log "Starting setup" |
| 181 | + add_sudo_user |
| 182 | + update_and_upgrade |
| 183 | + install_php |
| 184 | + install_nginx |
| 185 | + install_mysql |
| 186 | + install_elasticsearch |
| 187 | + install_redis |
| 188 | + install_node |
| 189 | + set_permissions |
| 190 | + install_composer_dependencies |
| 191 | + install_node_packages |
| 192 | + setup_environment |
| 193 | + migrate_database |
| 194 | + log "Setup completed! Please make sure to update the .env file with correct database connection details and restart your server." |
| 195 | +} |
| 196 | + |
| 197 | +# Execute main function |
| 198 | +main |
0 commit comments