November 13th, 2024, posted in for_founders
by Adelina
LAMP is an acronym for a widely used stack of software - Linux, Apache, MySQL, PHP/Perl/Python - in building web applications. There are different variations of the stack, depending on the operating system: on Windows it’s called WAMP and on Mac it’s called MAMP. Lately, the components of this stack tend to vary, but it’s still widely used across web apps.
The components of the bundle are open-source and they refer to:
- An operating system: Linux or MacOS in this case
- A web server: Apache
- A relational database: MySQL
- A programming language: PHP or Perl or Python
How to install LAMP on Ubuntu
Prerequisites: Ubuntu version 18.04 or later
Step 1. Install Apache
First make sure the package list on the system is up to date and then Apache can be installed and the traffic has to be enabled. In the terminal write the command:
sudo apt update
- it might require your credentials for the execution of the command
sudo apt install apache2
- to install Apache
sudo service apache2 status
- to confirm that Apache is running
sudo ufw app list
- this command will adjust the firewall to allow web traffic
sudo ufw app info "Apache Full"
- to see the PORTs where the traffic is enabled
sudo ufw allow "Apache Full"
- to allow the traffic
Step 2. Install MySQL
MySQL it’s a relational database management system where you can store the information for your application.
Here are commands you need to run to install it:
sudo apt install mysql-server
- for installation
sudo systemctl start mysql.service
- to make sure the service is running
To configure and secure MySQL you should run:
sudo mysql_secure_installation
- helps with setting a password and with setting up security
To use MySQL is a simple command of:
sudo mysql -u root -p
Also you can create a new user and grant privileges if you don’t want to use the default root user. For easier use of MySQL, phpMyAdmin can be used to create DB, add tables, edit tables and rows, edit/add/delete columns, and create views.
Step 3. Install PHP
Now we need to install PHP and the extensions needed:
sudo apt install php libapache2-mod-php php-mysql
Or if you want to install a specific PHP version, in this example version 8 of PHP:
sudo apt install php8.0 libapache2-mod-php8.0
Probably you will need more extensions for your project so you can search for them by using the command:
sudo apt install php8.0-[extname]
Some commonly used extensions are:
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
Optional. Install phpMyAdmin
After you install your LAMP bundle you can install phpMyAdmin to work easier with MySQL and see your data clearer.
For Ubuntu 18.04
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
The next step is to select Apache2 as server for your MySQL configuration and to add your password to access phpMyAdmin.
For Ubuntu 20
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
sudo phpenmod mbstring
sudo systemctl restart apache2
Set up a user and credentials
For the root user use the command sudo mysql -u root -p and after you hit enter you will need to add your password
Also to have full privileges, you will need to run this in the command line:
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost';
FLUSH PRIVILEGES;
EXIT
Now you can access phpMyAdmin from your browser by going to: http://localhost/phpmyadmin.
How to install LAMP on MacOS
As with LAMP on Ubuntu, Mac users that work with PHP files and projects need to install the (L)AMP bundle for local testing.
Prerequisites: Have Homebrew installed.
Step 1. Install Apache
First, it’s a good idea to check if Apache is already installed because on some versions it is. To find if you already have Apache installed and which version, the command httpd -v
will help you.
If Apache is already installed, then you should check its status by running:
sudo apachectl configtest
To stop the services: sudo apachectl stop
To stop the services: sudo apachectl start
To restart the services: sudo apachectl restart
If the web server is not pre-installed you will need to run the following commands to install:
- create in your root a new folder
- mkdir ~/Sites
- Also create an index. html file for testing with -
echo "Apache Testing" >~/Sites/index.html
brew update
- to update the package installerbrew install httpd
- to install the new version. Note that https is the same as Apache2.0.brew services start httpd
- it configures the Apache server to start automatically.
Now Apache is installed and running. To check if everything works as expected you should go to htpp://localhost:8080
Configuration
- Open the following file in an editor:
/usr/local/etc/httpd/httpd.conf
- You can change listen 8080 to listen 80
- Make the following changes:
1. Change Document Root “/usr/local/www” to Document Root “/Users/YOUR_USERNAME/Sites” (by typing the command whoami you can see your username)
2. Change <Directory “usr/local/www” to <Directory “Users/YOUR_USERNAME/Sites”> and here also change from AllowOverride None to AllowOverride All
3. Uncomment #LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
4. Change User _www to YOUR_USERNAME
5. Change Group _www to Group staff
6. Uncomment and change to update the PORT #ServerName www.example.com:8080 to ServerName www.example.com:80
Now you will need to restart Apache, go to your localhost and there you can find the documents from your Sites directory.
Step 2. Install PHP
PHP is also installed with the help of Homebrew
brew install [email protected]
- to install PHP version 8 (you can also install different PHP versions and choose which version to use)
brew services start [email protected]
- to start the services
php -v
- check your PHP version you run
Configuration
- Open the following file in an editor
/usr/local/etc/httpd/httpd.conf
- After the LastModule in the file insert, depending on your version, add
LoadModule php8_module /usr/local/opt/php/lib/httpd/modules/libphp8.so
- Change DirectoryIndex index.html to DirectoryIndex index.html index.php
- Insert the following between previously update and htaccess/htpasswd:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
At this point you should restart Apache. You can also create a .php file in your Sites directory to make sure it’s working and test it by going to the http://localhost/your_file.php.
Step 3. Install MySQL
To work with stored data you need to install MySQL
brew install mysql
- for installation
brew services start mysql
- to start the server
mysql_secure_installation
- run the command to follow the process of securing the server
mysql -u root -p
- to connect to the server, the name is root; after this command you will need to add your password
Now you can connect your application to your MySQL Database.
And there it was, that’s how to install LAMP on Ubuntu and MacOS. Now you can freely use this package in your projects.