Skip to content

Instantly share code, notes, and snippets.

@jonmunson
Created August 22, 2024 13:35
Show Gist options
  • Save jonmunson/16a7ebc645e172f6cc1f78d45e2c1840 to your computer and use it in GitHub Desktop.
Save jonmunson/16a7ebc645e172f6cc1f78d45e2c1840 to your computer and use it in GitHub Desktop.
LAMP stack with VHOST and GIT install script

LAMP Stack with Vhosts and Git Setup Script

This script installs a LAMP stack, configures virtual hosts, and sets up Git for version control on your Ubuntu server. The script is reusable; simply change the domain name at the beginning of the script to set up new projects.

Instructions

1. Set the DOMAIN_NAME Variable

At the top of the script, set the DOMAIN_NAME variable to the desired domain name.

```bash DOMAIN_NAME="yourdomain.com" ```

2. Save the Script

Save the script as install_lamp_vhosts_git.sh:

```bash nano install_lamp_vhosts_git.sh ```

3. Make the Script Executable

Make the script executable by running:

```bash chmod +x install_lamp_vhosts_git.sh ```

4. Run the Script

Run the script to install and configure the LAMP stack, virtual host, and Git:

```bash ./install_lamp_vhosts_git.sh ```

Script Content

```bash #!/bin/bash

Set the domain name as a variable

DOMAIN_NAME="janus.apexl"

Update the package index

echo "Updating package index..." sudo apt update

Install Apache

echo "Installing Apache..." sudo apt install -y apache2

Start and enable Apache

echo "Starting and enabling Apache..." sudo systemctl start apache2 sudo systemctl enable apache2

Install MySQL Server

echo "Installing MySQL Server..." sudo apt install -y mysql-server

Secure MySQL installation

echo "Securing MySQL installation..." sudo mysql_secure_installation <<EOF

y 0 y y y EOF

Set MySQL root password

MYSQL_ROOT_PASSWORD=$(openssl rand -base64 12) echo "Setting MySQL root password..." sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$MYSQL_ROOT_PASSWORD'; FLUSH PRIVILEGES;"

Install PHP and necessary modules

echo "Installing PHP and modules..." sudo apt install -y php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc

Install Git

echo "Installing Git..." sudo apt install -y git

Create a directory for vhosts

echo "Creating directory for vhosts..." sudo mkdir -p /var/www/$DOMAIN_NAME/public_html sudo chown -R $USER:$USER /var/www/$DOMAIN_NAME/public_html sudo chmod -R 755 /var/www

Create a sample index.php file in the vhost directory

echo "" > /var/www/$DOMAIN_NAME/public_html/index.php

Create a new virtual host configuration file

VHOST_CONF="/etc/apache2/sites-available/$DOMAIN_NAME.conf"

echo "Creating virtual host configuration file..." sudo cat <<EOL | sudo tee $VHOST_CONF <VirtualHost *:80> ServerAdmin webmaster@$DOMAIN_NAME ServerName $DOMAIN_NAME ServerAlias www.$DOMAIN_NAME DocumentRoot /var/www/$DOMAIN_NAME/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/$DOMAIN_NAME/public_html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
EOL

Enable the new virtual host and disable the default site

echo "Enabling the new virtual host and disabling the default site..." sudo a2ensite $DOMAIN_NAME.conf sudo a2dissite 000-default.conf

Enable Apache mod_rewrite

echo "Enabling Apache mod_rewrite..." sudo a2enmod rewrite

Restart Apache to apply changes

echo "Restarting Apache..." sudo systemctl restart apache2

Initialize a Git repository in the vhost directory

echo "Initializing a Git repository in the vhost directory..." cd /var/www/$DOMAIN_NAME/public_html git init echo "Initial commit" > README.md git add . git commit -m "Initial commit"

Test if Apache is running

APACHE_STATUS=$(systemctl is-active apache2) if [ "$APACHE_STATUS" = "active" ]; then echo "Apache is running successfully." else echo "Apache failed to start." fi

Test if PHP is working

PHP_TEST=$(curl -s http://$DOMAIN_NAME | grep -o "Hello, World!") if [ "$PHP_TEST" = "Hello, World!" ]; then echo "PHP is working correctly." else echo "PHP is not working. Please check your configuration." fi

Display MySQL root password and other details

echo echo "LAMP stack installation with vhosts and Git setup completed!" echo "MySQL root password: $MYSQL_ROOT_PASSWORD" echo "You can access your web server at: http://$DOMAIN_NAME/" echo "Your Git repository has been initialized in /var/www/$DOMAIN_NAME/public_html"

echo echo "NOTE: For security, consider creating a new MySQL user with limited privileges instead of using the root account for your web applications." ```

Notes

  • This script is reusable for different projects. Just update the DOMAIN_NAME variable to match the domain you are working with.
  • Ensure that your DNS is correctly configured to resolve the DOMAIN_NAME to your server.

Enjoy your LAMP stack setup with virtual hosts and Git!

#!/bin/bash
# Set the domain name as a variable
DOMAIN_NAME="example.com"
# Update the package index
echo "Updating package index..."
sudo apt update
# Install Apache
echo "Installing Apache..."
sudo apt install -y apache2
# Start and enable Apache
echo "Starting and enabling Apache..."
sudo systemctl start apache2
sudo systemctl enable apache2
# Install MySQL Server
echo "Installing MySQL Server..."
sudo apt install -y mysql-server
# Secure MySQL installation
echo "Securing MySQL installation..."
sudo mysql_secure_installation <<EOF
y
0
y
y
y
EOF
# Set MySQL root password
MYSQL_ROOT_PASSWORD=$(openssl rand -base64 12)
echo "Setting MySQL root password..."
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$MYSQL_ROOT_PASSWORD'; FLUSH PRIVILEGES;"
# Install PHP and necessary modules
echo "Installing PHP and modules..."
sudo apt install -y php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc
# Install Git
echo "Installing Git..."
sudo apt install -y git
# Create a directory for vhosts
echo "Creating directory for vhosts..."
sudo mkdir -p /var/www/$DOMAIN_NAME/public_html
sudo chown -R $USER:$USER /var/www/$DOMAIN_NAME/public_html
sudo chmod -R 755 /var/www
# Create a sample index.php file in the vhost directory
echo "<?php echo 'Hello, World!'; ?>" > /var/www/$DOMAIN_NAME/public_html/index.php
# Create a new virtual host configuration file
VHOST_CONF="/etc/apache2/sites-available/$DOMAIN_NAME.conf"
echo "Creating virtual host configuration file..."
sudo cat <<EOL | sudo tee $VHOST_CONF
<VirtualHost *:80>
ServerAdmin webmaster@$DOMAIN_NAME
ServerName $DOMAIN_NAME
ServerAlias www.$DOMAIN_NAME
DocumentRoot /var/www/$DOMAIN_NAME/public_html
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/$DOMAIN_NAME/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOL
# Enable the new virtual host and disable the default site
echo "Enabling the new virtual host and disabling the default site..."
sudo a2ensite $DOMAIN_NAME.conf
sudo a2dissite 000-default.conf
# Enable Apache mod_rewrite
echo "Enabling Apache mod_rewrite..."
sudo a2enmod rewrite
# Restart Apache to apply changes
echo "Restarting Apache..."
sudo systemctl restart apache2
# Initialize a Git repository in the vhost directory
echo "Initializing a Git repository in the vhost directory..."
cd /var/www/$DOMAIN_NAME/public_html
git init
echo "Initial commit" > README.md
git add .
git commit -m "Initial commit"
# Test if Apache is running
APACHE_STATUS=$(systemctl is-active apache2)
if [ "$APACHE_STATUS" = "active" ]; then
echo "Apache is running successfully."
else
echo "Apache failed to start."
fi
# Test if PHP is working
PHP_TEST=$(curl -s http://$DOMAIN_NAME | grep -o "Hello, World!")
if [ "$PHP_TEST" = "Hello, World!" ]; then
echo "PHP is working correctly."
else
echo "PHP is not working. Please check your configuration."
fi
# Display MySQL root password and other details
echo
echo "LAMP stack installation with vhosts and Git setup completed!"
echo "MySQL root password: $MYSQL_ROOT_PASSWORD"
echo "You can access your web server at: http://$DOMAIN_NAME/"
echo "Your Git repository has been initialized in /var/www/$DOMAIN_NAME/public_html"
echo
echo "NOTE: For security, consider creating a new MySQL user with limited privileges instead of using the root account for your web applications."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment