How to Install and Configure Apache, PHP, and MySQL on Ubuntu using Python
The LAMP stack (Linux, Apache, MySQL, PHP) is a popular web development platform used by developers to create dynamic and interactive websites. In this tutorial, we will show you how to install and configure Apache, PHP, and MySQL on Ubuntu using Python.
To get started, open a terminal window and run the following command to update your system:
sudo apt-get update
Then, install Apache, PHP, and MySQL using the following command:
sudo apt-get install -y apache2 php libapache2-mod-fcgid php-fpm php-mysql php-xml php-zip php-mbstring php-gd mysql-server mysql-client
Once the installation is complete, enable the Apache modules using the following command:
sudo a2enmod -y proxy_fcgi http2
Configure FCGID by adding the following lines to the end of the /etc/apache2/mods-available/fcgid.conf
file:
AddHandler fcgid-script .php
FcgidConnectTimeout 20
FcgidIOTimeout 180
FcgidBusyTimeout 180
FcgidMaxProcesses 200
FcgidMaxRequestsPerProcess 500
FcgidMinProcessesPerClass 1
FcgidIdleTimeout 60
FcgidMaxProcessesPerClass 25
FcgidMinProcesses 1
FcgidInitialEnv RAILS_ENV production
FcgidMaxRequestLen 536870912
Next, configure the Apache virtual host by adding the following lines to the end of the /etc/apache2/sites-available/000-default.conf
file:
<FilesMatch \\.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost/"
</FilesMatch>
Configure PHP by adding the following lines to the end of the /etc/php/7.4/fpm/php.ini
file:
memory_limit = 512M max_execution_time = 90 upload_max_filesize = 512M post_max_size = 512M display_errors = Off log_errors = On error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
Finally, install and configure Composer by running the following commands:
sudo curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
sudo rm composer-setup.php
Secure MySQL by running the following command:
sudo mysql_secure_installation
All of these commands can be automated using Python. Here is a Python script that installs and configures Apache, PHP, and MySQL, and sets PHP configuration parameters for a production environment:
import subprocess
php_version = '7.4'
# Install Apache, PHP, and MySQL
subprocess.run(["sudo", "apt-get", "update"], check=True)
subprocess.run(["sudo", "apt-get", "install", "-y", "apache2"], check=True)
subprocess.run(["sudo", "apt-get", "install", "-y", "php" + php_version, "libapache2-mod-fcgid", "php-fpm", "php-mysql", "php-xml", "php-zip", "php-mbstring", "php-gd"], check=True)
subprocess.run(["sudo", "apt-get", "install", "-y", "mysql-server", "mysql-client"], check=True)
# Enable Apache modules
subprocess.run(["sudo", "a2enmod", "-y", "proxy_fcgi", "http2"], check=True)
# Configure FCGID
with open('/etc/apache2/mods-available/fcgid.conf', 'a') as conf_file:
conf_file.write('AddHandler fcgid-script .php\nFcgidConnectTimeout 20\nFcgidIOTimeout 180\nFcgidBusyTimeout 180\nFcgidMaxProcesses 200\nFcgidMaxRequestsPerProcess 500\nFcgidMinProcessesPerClass 1\nFcgidIdleTimeout 60\nFcgidMaxProcessesPerClass 25\nFcgidMinProcesses 1\nFcgidInitialEnv RAILS_ENV production\nFcgidMaxRequestLen 536870912\n')
# Configure Apache virtual host
with open('/etc/apache2/sites-available/000-default.conf', 'a') as conf_file:
conf_file.write('<FilesMatch \\.php$>\nSetHandler "proxy:unix:/run/php/php' + php_version + '-fpm.sock|fcgi://localhost/"\n</FilesMatch>\n')
# Configure PHP
with open('/etc/php/' + php_version + '/fpm/php.ini', 'a') as conf_file:
conf_file.write('memory_limit = 512M\nmax_execution_time = 90\nupload_max_filesize = 512M\npost_max_size = 512M\n')
conf_file.write('display_errors = Off\nlog_errors = On\nerror_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT\n')
# Install and configure Composer
subprocess.run(["sudo", "curl", "-sS", "https://getcomposer.org/installer", "-o", "composer-setup.php"], check=True)
subprocess.run(["sudo", "php", "composer-setup.php", "--install-dir=/usr/local/bin", "--filename=composer"], check=True)
subprocess.run(["sudo", "rm", "composer-setup.php"], check=True)
# Secure MySQL
subprocess.run(["sudo", "mysql_secure_installation"], check=True)
This script automates the process of installing and configuring Apache, PHP, and MySQL on Ubuntu. It also sets PHP configuration parameters for the environment and installs and configures Composer.
You can run this script using python
script.py
or python3 script.py
command in the terminal.