Install Lamp: Apache, PHP, MySQL and PHPMyAdmin on MacOS

- Andrés Cruz

En español
Install Lamp: Apache, PHP, MySQL and PHPMyAdmin on MacOS

In this video we are going to see how to install our LAMP environment on Mac OS, which, although it should be a simple procedure, unfortunately, on Mac OS, the matter is a bit complicated and is more similar to installing these web technologies on a Linux system than on a Mac; a point in favor of Windows where we can easily install this entire system using an .exe.

Manage and install Apache, PHP and MySQL independently

So we have to manage each web element that we want to install or make work independently as we will see in this guide.

Enable apache

The first thing we have to do is run our Apache; A point in favor is that it is already installed by default, at least in the latest versions of Mac OS; so just run the following command:

sudo apachectl start

And you will see a screen like the following where you will see your Apache server fully working:

sudo apachectl start

The next thing we will do is go to our browser and place our typical http://localhost/ in the address bar of your browser and hope that everything goes well:

Apache funciona

So far so good and nothing complicated.

Enable the PHP module in our Apache

The next thing we will do is go to our /etc/apache2/httpd.conf at the following address and with the following command:

sudo nano /etc/apache2/httpd.conf

*For that we use the following command with the text editor already integrated in our terminal.

Apache archivo de configuración

And uncomment the module that corresponds to PHP from our configuration file; as we do in Linux:

Activar PHP7 en Apache

After this we save and of course we restart our Apache server with the following command:

sudo apachectl restart

Let's go to the following address:

Library/WebServer/Documents

And we create a PHP file with the name that you want and we place the typical phpinfo() and you should see a screen like the following:

php_info()

Install MySQL on your Mac OS

To install MySQL on our web server on our Mac, we have to go to the official MySQL site which is the following link and we click where it says Download and download our MySQL:

web MySQL
web MySQL descarga

A few moments later we start the installation and that's it, very careful with remembering the password that you put that we are going to use it later; In our System Preferences we will see that a new icon or service that corresponds to MySQL now appears:

Preferencias sistema
Preferencias sistema

The next thing we are going to do is finish configuring our MySQL, for that we use the following command:

Mac-mini-de-Andres:phpmyadmin andrescruz$ sudo mkdir /var/mysql /var/mysql/mysql.sock Mac-mini-de-Andres:phpmyadmin andrescruz$ sudo ln -s /tmp/mysql.sock

Then we go to the following address:

Ruta MySQL

And we give it the following command:

sudo ./mysql -u root -p

Or

sudo mysql -u root -p
Sistema de MySQL

As you can see, we are entering our database system indicating the name and password that you configured at the time of installation.

If it doesn't work for you and gives you an error like the following:

mysql command not found

That is, if it does not find the MySQL instance, you can try placing the boot in the PATH of your Mac; for this reason the installation did not add the MySQL PATH to your system PATH; for that simply write:

PATH=$PATH:/usr/local/mysql/bin

Which is where the installation PATH of your MySQL is (still very pending to check if that is your installation PATH); now your PATH will be like the following:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:

Once you can log in to the MySQL system, run the following command:

ALTER USER root@'localhost' IDENTIFIED BY 'root';

In this way we modify the password of our database.

Install PhpMyAdmin

Of course, PhpMyAdmin is the quintessential manager of our database that cannot be missing and we are also going to install it; for this we go to the following link and download it; then we unzip it and move to the following address:

/Library/WebServer/Documents

We change the permissions so that we can work with it.

cd /Library/WebServer/Documents
sudo chmod o+x phpmyadmin/

We create a folder for the configurations (in the same directory as phpmyadmin):

sudo mkdir config
sudo chmod o+x config

In our browser we go to:

http://localhost/phpmyadmin/setup/

We give New Server (New server):

PHPmyAdmin configurar

And then in Authentication where we establish the password of our MySQL system:

PHPmyAdmin configurar

We hit the download button, it downloads a file that we must copy to the config folder that we created earlier.

And with this we will have our server working perfectly and we will be able to enter our database from:

http://localhost/phpmyadmin

mysqli_real_connect(): (HY000/2002): No such file or directory

If it gives you the following error:

mysqli_real_connect(): (HY000/2002): No such file or directory

You can try with the following command:

alter user 'root'@'localhost' identified with mysql_native_password by 'root';

Enable MOD_Rewrite on Mac OS

Surely once you have your LAMP 100% functional on your Mac, the next thing you want is to enable the URL writing mode, also called mod_rewrite, which we have already talked about before, what you have to do are a couple of very simple changes all in the httpd.conf file that you can edit with the following command:

sudo nano /etc/apache2/httpd.conf

With this, you should locate the commented module of:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

In my case it is here:

And uncomment it, that is, remove the #.

After this, you have to place a Directory tag like the following:

<Directory "/Library/WebServer/Documents">
    ***Otras cosas
    AllowOverride None
***Otras cosas
</Directory>

And replace the None with an All:

<Directory "/Library/WebServer/Documents">
    ***Otras cosas
    AllowOverride All
***Otras cosas
</Directory>

Then we restart Apache and with this you will have the URL override module working perfectly and you will be able to use, for example, frameworks such as CodeIgniter that require this feature in order to function correctly:

sudo apachectl restart

Install and configure composer

The next thing we are going to see is how we can configure composer on our MAC; for that, we are going to follow the official documentation in which it tells us that the first thing we have to do is go to the official documentation page and execute a command similar to this one; we recommend that you run the one indicated in the official documentation in case at any time it changes in something: 

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e5325b19b381bfd88ce90a5ddb7823406b2a38cff6bb704b0acc289a09c8128d4a8ce2bbafcd1fcbdc38666422fe2806') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

What this command does is download the installer, check the download, run the installer and finally delete the installer; at the end, we have a file named as:

Composer.phar

What is our file that we can use to install or use any package by composer.

Install composer globally

Now, surely having a loose file lying around on our Mac is not the best idea, what if we add it to the system path; for that we simply move it to the designated path:

mv composer.phar /usr/local/bin/composer

Now, we can use composer globally on our team.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.