Apache LAMP Install Script

Apache LAMP Install Script

Here’s a full Apache LAMP Install Script for setting up aa LAMP stack on Ubuntu (assuming Linux is excluded from the setup), including the installation and configuration of Apache, PHP, MySQL, and phpMyAdmin. The script also includes basic Apache configurations, enabling modules like mod_rewrite, and configuring phpMyAdmin with secure settings.

Full Apache LAMP Install Script
(for Ubuntu-based systems):



#!/bin/bash

# Update and upgrade the system
sudo apt update -y
sudo apt upgrade -y

# Add PPA for PHP and Apache
echo "Adding PPA repositories for PHP and Apache..."
sudo add-apt-repository ppa:ondrej/php -y
sudo add-apt-repository ppa:ondrej/apache2 -y
sudo apt update -y

# Install Apache2
echo "Installing Apache2..."
sudo apt install apache2 -y

# Install PHP and commonly used extensions
echo "Installing PHP and extensions..."
sudo apt install php libapache2-mod-php php-cli php-mysql php-curl php-gd php-xml php-mbstring php-zip php-soap -y

# Install MySQL Server and secure the installation
echo "Installing MySQL Server..."
sudo apt install mysql-server -y

# Run MySQL Secure Installation
echo "Securing MySQL installation..."
sudo mysql_secure_installation

# Install phpMyAdmin
echo "Installing phpMyAdmin..."
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y

# Link phpMyAdmin to Apache web directory
echo "Configuring phpMyAdmin..."
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

# Set permissions for phpMyAdmin
echo "Setting permissions for phpMyAdmin..."
sudo chown -R www-data:www-data /usr/share/phpmyadmin
sudo chmod -R 755 /usr/share/phpmyadmin

# Enable Apache modules
echo "Enabling Apache modules..."
sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod ssl

# Set up basic Apache configurations (security headers, etc.)
echo "Configuring Apache settings..."
echo '
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-XSS-Protection "1; mode=block"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "no-referrer"
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php [QSA,L]
</IfModule>
' | sudo tee /etc/apache2/conf-available/security_headers.conf > /dev/null

# Enable custom security headers configuration
sudo a2enconf security_headers

# Enable and restart Apache and MySQL services
echo "Restarting Apache and MySQL..."
sudo systemctl restart apache2
sudo systemctl restart mysql

# Set MySQL to start on boot
echo "Ensuring MySQL starts on boot..."
sudo systemctl enable mysql

# Test Apache and MySQL installation
echo "Testing Apache and MySQL..."
sudo systemctl status apache2
sudo systemctl status mysql

# Configure phpMyAdmin with MySQL (Optional, run if needed)
echo "Configuring phpMyAdmin to work with MySQL..."
# Create a user for phpMyAdmin in MySQL
sudo mysql -u root -p -e "CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY 'phpmyadminpassword';"
sudo mysql -u root -p -e "GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;"

echo "LAMP stack installation complete!"


Breakdown of the Apache LAMP Install Script:

  1. System Updates:
    • Updates the package list and upgrades the system to ensure it is up-to-date.
  2. PPA for PHP and Apache:
    • Adds the PPA repositories for the latest PHP and Apache versions (ppa:ondrej/php and ppa:ondrej/apache2).
  3. Apache2 Installation:
    • Installs the Apache web server.
  4. PHP Installation:
    • Installs PHP along with some common PHP extensions (like MySQL, CURL, GD, MBString, XML, and SOAP).
  5. MySQL Installation and Security Setup:
    • Installs MySQL and runs the mysql_secure_installation script to secure the MySQL installation (you’ll need to set a root password and answer security questions).
  6. phpMyAdmin Installation:
    • Installs phpMyAdmin and relevant PHP extensions. It then configures it to be accessible via the Apache web server.
  7. Enabling Apache Modules:
    • Enables the mod_rewrite, mod_headers, and mod_ssl modules for security and functionality.
  8. Apache Basic Configuration:
    • Sets up HTTP security headers and enables the mod_rewrite rule to handle URL rewriting in Apache.
  9. Restart Services:
    • Restarts Apache and MySQL services to apply changes.
  10. Test:
    • Verifies that Apache and MySQL services are running properly.
  11. MySQL User for phpMyAdmin (Optional):
    • Creates a user for phpMyAdmin in MySQL with the necessary privileges. You can customize the password and user details.

Additional Notes:

  • MySQL Secure Installation: This script will invoke the mysql_secure_installation command during execution. You will be prompted to configure your MySQL root password and set other security options interactively.
  • phpMyAdmin: By default, phpMyAdmin will be accessible at http://your-server-ip/phpmyadmin after running this script. Make sure to adjust any security settings (e.g., .htaccess protection) for production environments.
  • Permissions: The script ensures that phpMyAdmin has proper file permissions to function correctly under the web server’s user (www-data).