Skip to content

Instantly share code, notes, and snippets.

@suvozy
Last active July 5, 2023 12:29
Show Gist options
  • Save suvozy/6dda7971e240f0a3f282 to your computer and use it in GitHub Desktop.
Save suvozy/6dda7971e240f0a3f282 to your computer and use it in GitHub Desktop.
PHP Dev on Mac OS X 10.10 (PHP, MySQL, Apache, Composer)

Apache

to start it

sudo apachectl start

to stop it

sudo apachectl stop

to restart it

sudo apachectl restart

To find the Apache version

httpd -v

It works!

http://localhost

troubleshooting Apache to see if there is anything wrong in its config file by running

apachectl configtest

System Level Web Root

http://localhost/

The files are shared in the filing system at – /Library/WebServer/Documents/

User Level Root ~/Sites

Add a .conf

cd /etc/apache2/users/
sudo nano suvozit.conf

Add the content

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

Set permission

sudo chmod 644 suvozit.conf

Allow some modules:

sudo nano /etc/apache2/httpd.conf

Make sure these 3 modules are uncommented (the first 2 should 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

and

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

Then open another Apache config file and uncomment another file:

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

And uncomment:

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

Restart Apache for the new file to be read:

sudo apachectl restart

Then this user level document root will be viewable at: http://localhost/~suvozit/

Get php running

sudo nano /etc/apache2/httpd.conf

Uncomment

LoadModule php5_module libexec/apache2/libphp5.so

Restart Apache

sudo apachectl restart

Override .htaccess and allow URL Rewrites

sudo nano /etc/apache2/httpd.conf

Allow any .htaccess files used to override the default settings
Change AllowOverride None to AllowOverride All

# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All

Uncomment

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Restart Apache

sudo apachectl restart

Test it!

mkdir /Users/suvozit/Sites/welcome
sudo nano /Users/suvozit/Sites/welcome/index.php

Paste it

<?php phpinfo(); ?>

URL

http://localhost/~suvozit/welcome

Permissions

To run a website with no permission issues it is best to set the web root and its contents to be writeable by all, since it’s a local development it should’nt be a security issue.

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

If you are concerned about security then instead of making it world writeable you can set the owner to be Apache _www

sudo chown -R _www ~/Sites/welcome

Xcode

Download and install the Xcode Command Line Tools:
https://developer.apple.com/downloads/
or run the command:

xcode-select --install

Homebrew

# ruby -e "$( replace the libphp5.so direccurl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# OS X El Capitan 10.11
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Check for any conflicts or problems

brew doctor

Update and upgrade its formulas in case you already had Homebrew installed before:

brew update && brew upgrade

PHP-FPM

brew tap homebrew/dupes
brew tap homebrew/php

Now install it with the following arguments:

brew install php56 --with-fpm --with-mysql --with-mcrypt

Setup PHP CLI binary

Check version

php -v

Update the $PATH environment variable of shell profile.

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile
. ~/.bash_profile

Now run again:

php -v

PHP info

php -i 

Setup auto start:

Create a folder for our LaunchAgents and symlink the start/stop service:

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/

And start PHP-FPM:

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

Make sure PHP-FPM is listening on port 9000:

lsof -Pni4 | grep LISTEN | grep php

The output should look something like this:

php-fpm   69659  frdmn    6u  IPv4 0x8d8ebe505a1ae01      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   69660  frdmn    0u  IPv4 0x8d8ebe505a1ae01      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   69661  frdmn    0u  IPv4 0x8d8ebe505a1ae01      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   69662  frdmn    0u  IPv4 0x8d8ebe505a1ae01      0t0  TCP 127.0.0.1:9000 (LISTEN)    

Configure Apache

sudo nano /etc/apache2/httpd.conf

Replace

#LoadModule php5_module libexec/apache2/libphp5.so

Uncomment it by removing #, and replace the libphp5.so directory to our new PHP version. The updated line should look like this:

LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so

restart Apache

sudo apachectl restart

Check version at

http://localhost/~suvozit/welcome

MySQL

Next step is to install MySQL:

brew install mysql

Setup auto start

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

And start the database server:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Setup CLI

echo 'PATH="/usr/local/mysql/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

Check version

mysql -v

Use these commands to start / stop mysql server:

mysql.server stop
mysql.server start
mysql.server restart

Secure the installation

To seure our MySQL server, we'll exececute the provided secure_mysql_installation binary to change the root password, remove anonymous users and disbale remote root logins:

mysql_secure_installation
Securing the MySQL server deployment.

Enter password for root user: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:

Nopes. Not required.

> Enter current password for root (enter for none):

Press enter since you don't have one set.

> Change the root password? [Y/n]

Press enter, choose a root password, add it in your 1Password keychain and enter it here.

> Remove anonymous users? [Y/n]

Yes. They are not necessary.

> Disallow root login remotely? [Y/n]

Yes. No need to log in as root from any other IP than 127.0.0.1.

> Remove test database and access to it? [Y/n]

Yes. We don't need the testing tables.

> Reload privilege tables now? [Y/n]

Reload the privilege table to ensure all changes made so far will take effect.

Test connection

mysql -uroot -p

Enter your root password and you should see the MySQL console:

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
Quit the session with \q:

mysql> \q
Bye

Fix the 2002 MySQL Socket error

Fix the looming 2002 socket error – which is linking where MySQL places the socket and where OSX thinks it should be, MySQL puts it in /tmp and OSX looks for it in /var/mysql the socket is a type of file that allows mysql client/server communication.

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

Set Timezone

For MySQL 5.7 on Mac OS X El Capitan: /usr/local/opt/mysql/my.cnf
Copy default conf from /usr/local/opt/mysql/support-files/my-default.cnf

cp /usr/local/opt/mysql/support-files/my-default.cnf /usr/local/opt/mysql/my.cnf
sudo nano /usr/local/opt/mysql/my.cnf

insert the following line:

default_time_zone='+00:00'
innodb_autoinc_lock_mode=0

restart mysql server

mysql.server restart

Test

$ mysql -uroot -p
mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| +00:00             | +00:00              |
+--------------------+---------------------+
1 row in set (0.00 sec)

phpMyAdmin

Install autoconf, which is needed for the installation of phpMyAdmin:

brew install autoconf

Set $PHP_AUTOCONF:

echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile

Let's start with the installation of phpMyAdmin:

brew install phpmyadmin

Note that this formula will NOT install mysql. It is not required since you might want to get connected to a remote database server.

Webserver configuration example (add this at the end of your /etc/apache2/httpd.conf for instance) :

open the file to edit

sudo nano /etc/apache2/httpd.conf

paste the code

Alias /phpmyadmin /usr/local/share/phpmyadmin
<Directory /usr/local/share/phpmyadmin/>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  <IfModule mod_authz_core.c>
    Require all granted
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Allow from all
  </IfModule>
</Directory>

and restart apache

sudo apachectl restart

Then, open http://localhost/phpmyadmin

Setup

http://docs.phpmyadmin.net/en/latest/setup.html#using-setup-script

http://localhost/phpmyadmin/setup

or no-password install

cd /usr/local/share/phpmyadmin
mv config.inc.php config.inc.php.bak
cp config.sample.inc.php config.inc.php

Done

http://localhost/phpmyadmin

~/Sites as localhost

sudo nano /etc/apache2/httpd.conf

Modify

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
  Options FollowSymLinks Multiviews
  AllowOverride None
</Directory>

to:

DocumentRoot "/Users/suvozit/Sites"
<Directory "/Users/suvozit/Sites">
  Options Indexes FollowSymLinks Multiviews
  AllowOverride All
</Directory>

restart it

sudo apachectl restart

refresh

http://localhost

mcrypt

Installing mcrypt library

brew install mcrypt php56-mcrypt

Restart Apache:

sudo apachectl restart

curl

brew install --with-openssl curl

Composer

Install globally
https://getcomposer.org/doc/00-intro.md#globally

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Using Composer

cd ~/Sites/project_name
composer install
composer update

Folders

cd /tmp/
mkdir -m 0777 aws-cache
mkdir -m 0777 ci
mkdir -m 0777 ci/upload


rm -rf /tmp/ci
# rm -rf drag/n/drop-directory
# -r "recursive" -f "force" (suppress confirmation messages)

ls -a -l
# drwxrwxrwx   2 suvozit  wheel       68 Mar 18 14:58 aws-cache
# drwxrwxrwx   3 suvozit  wheel      102 Mar 18 14:58 ci

Cronjobs

  1. In Terminal: crontab -e
  2. Press i to go into vim's insert mode
  3. Type your cron jobs, for example:
[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]
0-59 * * * * echo "Hello World"
* * * * * cd ~/Sites/rime && php index.php controller method param1 param2
  1. Press Esc to exit vim's insert mode

  2. Type ZZ (must be capital letters) (Shift + Z + Z)

  3. Verify by using crontab -l

  4. Check log

cat /var/mail/suvozit
  1. Remove mail
sudo rm -f /var/mail/suvozit

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment