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.
At the top of the script, set the DOMAIN_NAME
variable to the desired domain name.
```bash DOMAIN_NAME="yourdomain.com" ```
Save the script as install_lamp_vhosts_git.sh
:
```bash nano install_lamp_vhosts_git.sh ```
Make the script executable by running:
```bash chmod +x install_lamp_vhosts_git.sh ```
Run the script to install and configure the LAMP stack, virtual host, and Git:
```bash ./install_lamp_vhosts_git.sh ```
```bash #!/bin/bash
DOMAIN_NAME="janus.apexl"
echo "Updating package index..." sudo apt update
echo "Installing Apache..." sudo apt install -y apache2
echo "Starting and enabling Apache..." sudo systemctl start apache2 sudo systemctl enable apache2
echo "Installing MySQL Server..." sudo apt install -y mysql-server
echo "Securing MySQL installation..." sudo mysql_secure_installation <<EOF
y 0 y y y EOF
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;"
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
echo "Installing Git..." sudo apt install -y git
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
echo "" > /var/www/$DOMAIN_NAME/public_html/index.php
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
<Directory /var/www/$DOMAIN_NAME/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
EOL
echo "Enabling the new virtual host and disabling the default site..." sudo a2ensite $DOMAIN_NAME.conf sudo a2dissite 000-default.conf
echo "Enabling Apache mod_rewrite..." sudo a2enmod rewrite
echo "Restarting Apache..." sudo systemctl restart apache2
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"
APACHE_STATUS=$(systemctl is-active apache2) if [ "$APACHE_STATUS" = "active" ]; then echo "Apache is running successfully." else echo "Apache failed to start." fi
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
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." ```
- 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!