Seaverns Web Development Coding Security Applications and Software Development Bex Severus Galleries Digital Art & Photography

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).
PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language designed primarily for web development.

Php

What is PHP?

PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language designed primarily for web development. It can be embedded into HTML, making it particularly suited for creating dynamic web pages. PHP code is executed on the server, generating HTML that is sent to the client’s browser.

What It’s Used For

PHP is versatile and can be used for various purposes:

  1. Web Development: PHP is commonly used to build dynamic websites and web applications. It processes server-side logic and interacts with databases to generate web pages dynamically.
  2. Content Management Systems (CMS): Many popular CMS platforms, such as WordPress, Joomla, and Drupal, are built using PHP. These platforms allow users to manage website content easily without needing extensive programming knowledge.
  3. E-commerce Platforms: PHP powers many e-commerce solutions like Magento and WooCommerce, providing functionality for online stores, including product management, shopping carts, and payment processing.
  4. Web Services: PHP is used to create APIs (Application Programming Interfaces) that allow different applications to communicate and exchange data.
  5. Database Management: PHP can interact with various databases (like MySQL, PostgreSQL) to handle data operations such as storage, retrieval, and manipulation.
  6. Server-Side Scripting: PHP handles tasks on the server before the page is sent to the user, such as form processing, session management, and file handling.

Institutions That Use PHP

PHP is utilized across various sectors and institutions:

  1. Tech Companies: Many technology firms use PHP for developing web applications and platforms. Companies like Facebook and Wikipedia have utilized PHP in their tech stacks.
  2. Educational Institutions: Universities and colleges use PHP to develop educational platforms, student management systems, and online learning tools.
  3. Government Agencies: Government websites and services often use PHP for web development due to its flexibility and ease of use.
  4. Nonprofits: Many nonprofit organizations use PHP-based systems to manage their websites, donation platforms, and community outreach tools.
  5. Businesses: From small businesses to large enterprises, PHP is used to develop company websites, intranets, and customer-facing applications.

Security and Vulnerabilities

Security:

  1. Access Control: PHP provides mechanisms to handle user authentication and authorization, though the implementation quality depends on the developer.
  2. Data Sanitization: Proper data sanitization and validation are essential in PHP to prevent common vulnerabilities like SQL injection and cross-site scripting (XSS).
  3. Secure Configuration: PHP allows for secure configurations, such as disabling dangerous functions and controlling error reporting to prevent sensitive information leakage.
  4. Regular Updates: PHP is actively maintained, with security patches and updates released to address vulnerabilities and improve security.

Vulnerabilities:

  1. SQL Injection: PHP applications that interact with databases can be vulnerable to SQL injection attacks if they do not use prepared statements or proper escaping techniques.
  2. Cross-Site Scripting (XSS): Improper handling of user input can lead to XSS attacks, where malicious scripts are executed in the user’s browser.
  3. Remote Code Execution: Vulnerabilities in PHP code or server configurations can potentially allow attackers to execute arbitrary code on the server.
  4. Deprecated Functions: Using outdated or deprecated PHP functions can expose applications to security risks. It is important to stay updated with the latest PHP versions and best practices.

Resources

Here are some useful resources for learning more about PHP:

  1. PHP Official Website – The main site for PHP, including downloads, documentation, and news.
  2. PHP Manual – Comprehensive documentation covering PHP functions, features, and usage examples.
  3. PHP The Right Way – A community-driven guide to best practices and modern PHP development.
  4. W3Schools PHP Tutorial – An interactive tutorial for learning PHP from basics to advanced topics.
  5. PHP Security Best Practices – Recommendations and guidelines for securing PHP applications.

This overview provides a detailed understanding of PHP, its uses, security considerations, and available resources for further learning.

Linux Apache MySQL Php LAMP Server

MySQL

What is MySQL?

MySQL is an open-source relational database management system (RDBMS) that is widely used for managing and organizing data in a structured manner. Developed and maintained by Oracle Corporation, MySQL uses Structured Query Language (SQL) to handle database tasks such as data retrieval, insertion, updating, and deletion.

What It’s Used For

MySQL is versatile and can be used in a variety of contexts:

  1. Web Applications: It’s commonly used in conjunction with PHP and Apache in the LAMP (Linux, Apache, MySQL, PHP/Perl/Python) stack for developing web applications.
  2. Data Storage: It stores data in a tabular format, which is suitable for applications requiring structured data storage, such as CRM systems, e-commerce sites, and content management systems (CMS).
  3. Analytics and Reporting: Businesses use MySQL to store and query large datasets, performing operations like complex joins, aggregations, and reporting.
  4. Application Development: Developers use MySQL for backend databases in applications due to its reliability and performance.
  5. Business Applications: It supports enterprise-level applications and ERP systems by managing large volumes of transactional data.

Institutions That Use MySQL

MySQL is used by a wide range of institutions:

  1. Tech Companies: Many tech giants and startups use MySQL, including Facebook, Twitter, and Google, for various internal systems and services.
  2. Educational Institutions: Universities and research organizations use MySQL for managing research data, student records, and educational content.
  3. Financial Institutions: Banks and financial services use MySQL for transactional data, customer management, and compliance-related applications.
  4. Government Agencies: Government departments use MySQL for managing public records, administrative data, and service delivery systems.
  5. Healthcare: Hospitals and clinics use MySQL for patient records, appointment scheduling, and medical data management.

Security and Vulnerabilities

Security:

  1. Access Control: MySQL supports user authentication and permissions, allowing administrators to control who can access or modify data.
  2. Encryption: It offers data-at-rest and data-in-transit encryption options to protect sensitive information.
  3. Audit Logging: It can log queries and changes to monitor and detect suspicious activity.
  4. Security Updates: Regular updates and patches are released to address security vulnerabilities.

Vulnerabilities:

  1. SQL Injection: Like other SQL-based systems, MySQL can be vulnerable to SQL injection attacks if applications do not properly sanitize user input.
  2. Misconfigurations: Incorrectly configured MySQL installations can lead to security issues, such as unauthorized data access.
  3. Outdated Versions: Running outdated versions without the latest security patches can expose the database to known vulnerabilities.
  4. Backup Security: If not properly secured, backup files can be a target for data breaches.

Resources

Here are some useful resources for learning more about MySQL:

  1. MySQL Official Website – The main site for downloads, documentation, and product information.
  2. MySQL Documentation – Comprehensive documentation covering installation, configuration, and usage.
  3. MySQL Tutorial – A resource for learning MySQL through tutorials and examples.
  4. MySQL Forums – A place to ask questions and engage with the MySQL community.
  5. MySQL Security Best Practices – Guidelines and recommendations for securing MySQL installations.

This overview should give you a solid understanding of MySQL, its uses, and its security aspects.

Linux Apache MySQL Php LAMP Server

LAMP Server and WordPress Installation and Removal Scripts

Installing Linux Apache, MySql and PhP also known as a LAMP Server can be an easily automated process and save you from  countless hours of headaches.

LAMP Server Installation Script (Dry Build – Test Before Running):


#!/usr/bin/env bash
###############################################
# #
# Basic Server Installer - K0NxT3D 2020 #
# DRY BUILD - DO NOT RUN #
# #
###############################################

# Configuration
#
# Variables:
INST_DIR="/var/www/html/" # Default Linux Apache2 Install Directory
PASQL="Your-MySql-Root-Password-Goes-Here" # Set the MySql root Password
BROWS="Your-Default-Browser" # firefox, google-chrome, google-chromium, safari
MYURL="localhost" # Default Setting
#
# END Configuration

# Begin The Basic Server Install
sudo apt-get install lamp-server^ -y
clear

# Configure MySql
sudo mysql -u root
echo ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$PASQL'; && set -v
echo FLUSH PRIVILEGES;&& set -v
echo quit && set -v
sudo service mysql restart && set -v
clear

# Install PhpMyAdmin
sudo apt-get install phpmyadmin -y
#sudo apt install phpmyadmin php-mbstring php-gettext
clear

# Install WordPress
cd $INST_DIR
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
cd wordpress
mv * $INST_DIR
cd $INST_DIR
rm -r wordpress
clear

# Finish WordPress Install
$BROWSE $MYURL

Lamp Server Removal Script:


#!/usr/bin/env bash
###############################################
# #
# Basic Server UnInstaller - K0NxT3D 2020 #
# DRY BUILD - DO NOT RUN #
# #
###############################################

# This will remove LAMP not Site Files.
sudo service apache2 stop
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
sudo apt remove apache2.*
sudo apt-get autoremove
whereis apache2
sudo rm -rf /etc/apache2

# This will remove PHP
sudo apt-get purge `dpkg -l | grep php7.2| awk '{print $2}' |tr "\n" " "`
sudo apt-get purge php7.*
sudo apt-get autoremove --purge
whereis php
sudo rm -rf /etc/php

# This will remove MYSql
sudo service mysql stop
sudo apt-get remove --purge *mysql\*
sudo apt-get remove --purge mysql-server mysql-client mysql-common -y
rm -rf /etc/mysql
sudo apt-get autoremove
sudo apt-get autoclean

sudo reboot

You can add custom lines to the install script and mirror them in the removal script as well.
This is just a basic installation to get a site up and running. I use PhPMyAdmin or my Hosting Cpanel to setup the user database for WordPress separately.