Instruction to Install Apache, MySQL, PHP on macOS Mojave 10.14

Instruction to Install Apache, MySQL, PHP on macOS Mojave 10.14
0

This tutorial will show you how to install Apache, MySQL, PHP on macOS Mojave 10.14. MacOS Mojave 10.14 has been released by Apple and it allows users to operate APM stack (Apache, MySQL, PHP). This post will help you set up the AMP stack using loaded Apache and PHP and download MySQL and phpMyAdmin as well. Let’s dive in and see how to do it.

Setting Stuff Up

This needs to be done in the Terminal which is found in the OS filing system at /Applications/Utilities/Terminal

In general, it is not actually too difficult for those who are not used to operating Terminal. You just need to simply enter the command and hit enter. If something goes wrong the system will give you feedback otherwise, your command is done. The prefix sudo is used to access applications secured in certain folders. Once you use sudo command, you have to enter your admin password or iCloud password.

In order to launch Apache web sharing

sudo apachectl start

to stop it

sudo apachectl stop

to restart it

sudo apachectl restart

To find the Apache version

httpd -v

It’s worth noting that, the Apache version works perfectly with macOS Mojave is Apache/2.4.34

After launching Apache – test to see if the web server is working in the browser – http://localhost – there will be an announcement “It Works!”.

If you don’t get the localhost test, you can try testing Apache to check if there is anything wrong in its config file by using the command line

apachectl configtest

You will receive the notification later on if something is wrong.

Document Root

The document root is literally the location of shared files, which is actually the same as the traditional names of ‘ public_html ‘ and ‘ htdocs ‘. MacOS has 2 web roots which are system level and user level. The user level one will give multiple accounts the permission to have their own web roots meanwhile system level is for all users. It is recommended to use the user level because you don’t have to authenticate as an admin user.

System Level Web Root

– the default system document root is still found at –

http://localhost/

The files are shared in the filing system at –

/Library/WebServer/Documents/

User Level Root

The other web root directory which is missing by default is the “Sites” folder in the User account. This takes a bit longer to set up but some users are very familiar to using it.

It is necessary to create a “Sites” folder at the root level of your account and then it will work. You have to make a few additional tweaks to get the “Sites” folder back up and running.

Add a “username.conf” filed under:

/etc/apache2/users/

If you don’t have one, then generate one named by the short username of the account with the suffix .conf , its location and permissions/ownership is best tackled by using the Terminal , the text editor nano would be the best tool to deal with this.

If you are familiar with modifying the config files with a text editor app, I recommend you to use a free app BBEdit which has the ability to open hidden system files.

Open Terminal, and enter the commands below

cd /etc/apache2/users

sudo nano username.conf

Then add the content below replaceusername’ by yours in the code below:

<Directory "Users/username/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>

Permissions on the file should be:

-rw-r--r-- 1 root wheel 298 Jun 28 16:47 username.conf

If not you need to change by using the command line:

sudo chmod 644 username.conf

Launch the main httpd.conf and use the command:

sudo nano /etc/apache2/httpd.conf

Ensure these modules are uncommented (the first 2 should already be on a clean install):

LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule include_module libexec/apache2/mod_include.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Once you have this file open also to get php running, uncomment the below:

LoadModule php7_module libexec/apache2/libphp7.so

Then uncomment this configuration file also in httpd.conf – which allows user home directories.

Include /private/etc/apache2/extra/httpd-userdir.conf

Then, save all your changes (Control + O in nano )

Afterward, open another Apache config file and uncomment another file:

sudo nano /etc/apache2/extra/httpd-userdir.conf

And next:

Include /private/etc/apache2/users/*.conf

Save all your changes (Control + O in nano )

Restart Apache for the new file to take effect:

sudo apachectl restart

Then this user level document root will be viewable at:

http://localhost/~username/

It should be noted that you should only see a directory tree like structure if the folder is empty.

Override .htaccess` and allow URL Rewrites

This is already taken care of at the Sites level webroot by following the previous step.

sudo nano /etc/apache2/httpd.conf

4

Also while here allow URL rewrites so your permalinks look clean, not ugly.

Uncomment in httpd.confshould be uncommented on a clean install

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

PHP

PHP 7.1.9 is loaded in this version of macOS Mojave and you can turn on it by uncommenting a line in the httpd.conf file.

sudo nano /etc/apache2/httpd.conf

Later on, use “control” + “w” to search within nano and search for ‘php’ this will land you on the right line then uncomment the line (remove the #):

LoadModule php7_module libexec/apache2/libphp7.so

Write out and Save using the nano shortcut keys at the bottom ‘control + o’ and ‘control + x’

Reload Apache using this command:

sudo apachectl restart

In order to check out PHP, generate a file name it “phpinfo.php” and store it in your document root with the contents below, then view it in a browser.

<?php phpinfo(); ?>

MySQL

You need to dowload from the MySQL site because MySQL doesn’t exist by default in macOS. The latest version of MySQL 8.0.12 is compatible with the public release of macOS. Use the macOS 10.13 (x86, 64-bit), DMG Archive version (works on macOS Mojave).

To download, you should skip signing up, and look for » No thanks, just take me to the downloads! – head directly to the download mirrors and download the software from a mirror which is closest to you.

Once the download is completed, launch the .dmg and run the installer.

I recommend you stick to legacy if you prefer using GUI wrapper like phpMyadmin, due to the fact that phpMyAdmin can’t connect with the newer encryptions as version 8 is entirely new.

After that, add a password for the MySQL root user.

Add Mysql to your path

Once you get the installation done, you need to add the mysql directory to your shell path to avoid using MySQL with typing the full path. You can generate the “.bash_profile” file using vi or nano in your home directory.

cd ; nano .bash_profile

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

Then quit the file by typing “control + x” and when asked to save the change by typing “y”. Next, reload the shell for the above to work directly.

source ~/.bash_profile

Change the MySQL root password

Stop MySQL

sudo /usr/local/mysql/support-files/mysql.server stop

Start it in safe mode:

sudo mysqld_safe --skip-grant-tables

Next up, open another shell/terminal window , and log in without a password as root :

mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Modify the text ‘MyNewPass’ to what you desire – and keep the single quotes.

\q

Start MySQL

sudo /usr/local/mysql/support-files/mysql.server start

Starting MySQL

You can then start the MySQL server from the System Preferences or via the command line.

Then use command line to start MySQL.

sudo /usr/local/mysql/support-files/mysql.server start

For searching the MySQL version from the terminal, type at the prompt:

/usr/local/mysql/bin/mysql -v -uroot -p

By doing this, you will access a shell interactive dialogue with MySQL, type \q to exit.

Fix the 2002 MySQL Socket error

sudo mkdir /var/mysql

sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

phpMyAdmin

Ensure that you already fixed the 2002 socket error if you haven’t done so from the MySQL section-

sudo mkdir /var/mysql

sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Download phpMyAdmin, then extract the file. Next, move the folder with its contents into the document root level after renaming folder to ‘phpmyadmin’.

Make the config folder

mkdir ~/Sites/phpmyadmin/config

Change the permissions

chmod o+w ~/Sites/phpmyadmin/config

Run the set up in the browser

http://localhost/~username/phpmyadmin/setup/ or http://localhost/phpmyadmin/setup/

10

You need to generate a new localhost mysql server connection, click new server .

Afterward, go to the Authentication tab and set the local MySQL root user and the password.
Add in the username “root”

Now access http://localhost/~username/phpmyadmin/ will now allow you to interact with your MySQL databases.

12

Permissions

In order to avoid the security problems during running a website, you should set the we root and its contents to be wirteable by all.

Assume that you have a site in the User Sites folder at the following location ~/Sites/testsite you would set it to be writeable like so:

sudo chmod -R a+w ~/Sites/testsite

If you worry about security then instead of making it world writeable you can set the owner to be Apache _www but when working on files you would have to authenticate more as admin you are “not” the owner, you would do this like so:

sudo chown -R _www ~/Sites/testsite

By doing this, you will set the contents recursively to be owned by the Apache user.

If you had the website stored at the System level Document root at say /Library/WebServer/Documents/testsite then it would have to be the latter:

sudo chown -R _www /Library/WebServer/Documents/testsite

Another more direct way to do this if you have a one user workstation is to change the Apache web user from _www to your account

That’s it! You have just sucessfully done the installation of the native AMP stack running on top of macOS Mojave.