Work on Mac

On February 26, 2017, in Other Online Technology, by James Liu

I am a 10 years old Windows user, but switch to Mac in this year. Lots of people say that Mac is so user friendly and easy to use. However, at the beginning, I don’t feel that, just like all other Windows users. The reason I switch from Windows to Mac is because of the iOS development. Of course I am also working on HTML, Javascript, PHP and Android. Therefore, I have to move all my daily work on Mac now. In this article, I will write some tips about how to use Mac, for new users who have been working on Windows for years.

Web Browser on Mac

On Windows, the built-in web Browser is Internet Explorer (IE), but on Mac, the default browser is Safari. There are some difference between them, but I will list them, as I am using Firefox and Chrome on Windows. So on Mac, I just use Safari for a few minutes to download the Firefox and Google Chrome. That’s it. Of course, I also use Safari for debugging hybrid iOS app. If you don’t know what is hybrid iOS app, please check this post:
https://jmsliu.com/3164/build-android-app-and-ios-app-with-backbone-and-cordova.html

Notepad++ Alternatives for Mac

Windows has notepad which you can use it to edit plain text file, but it is terrible. Instead of notepad, I am using Notepad++. It is a famous light weight text editor which is free. Notepad++ doesn’t have Mac version. On Mac, people will say sublime text. Unfortunately, it is not free. So I guess it could be the best one as I have never used it before, and I guess I will not use it ever if it is not free. There are also some free text editor like TextWrangler or Visual Studio Code. And I am using the second one. Please don’t ask me why, as I just tried it and it’s quite nice, so I continue to use it.

Photo Editor: GIMP

GIMP is the most famous Photoshop alternative software. It is free and only a little bit hard to use. Though it is not easy to use, I think you can get used to it after several hours.

Winrar Alternatives for Mac

To be honest, I don’t like Winrar, don’t like it at all. On Windows, I am using 7zip, an open source software for rar, zip, tar, gz file all in one. Again, 7zip doesn’t have Mac verion. On Mac, it has built-in zip app which you can easy to compress files by right click. You also can easy to unzip the zip file by double-clicking the zip file. But for RAR file, I have to download the command-line rar tool. Currently, Winrar only provides 32bit version for Mac. Here is the download link:
http://win-rar.com/predownload.html?spV=true&f=rarosx-5.4.0.tar.gz

Command line is the most powerful tool on Mac. Again, it is another headache problem for most Windows users. But please trust me, I love it very much now.

After downloading the rar command line tool, unzip it and put it to anywhere you like. I put it in my home application folder. Because rar/unrar app is not in the system path, we cannot directly use it in the command line yet. But it is not a problem. There are 2 ways to solve this problem.

  • Make a soft link in /usr/local/bin to unrar
  • To determine where to create the soft link, please use this command to check the system path first:

    echo $PATH

    Here is my system output.

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

    I decide to make the soft link in /usr/local/bin. There is a reason why I choose this folder. OS X EI Capitan and later includes “System Integrity Protection” that helps protect your Mac from malicious software. It prevents even root from changing things in /System, /usr, /bin, and /sbin. The only exception is /usr/local/bin. Now let’s do it with following command line:

    cd /usr/local/bin
    sudo ln -s ~/Applications/rar/unrar ./unrar
    

    Done!

  • Add rar folder in system path (I don’t choose this solution)

Finished? Of course not. This tutorial will be very long as Mac is not that easy to use for Windows users. I just finish my software installation. To be surprised, I only need 4 softwares to make money. In the next section, I will write something about building working environment, like setting up nginx, mysql, php and ssh.

Xcode

For developers working on Mac, Xcode is an essential app you must install first. Don’t ask why, just open your App Store on Mac, search Xcode and install.

Homebrew

Homebrew is the best package manager for Mac. It requires Xcode. If you already install the Xcode, please run following command in the command line:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

It’s better to run this command before installing any software:

brew doctor

Preceding command will detect problems and try to solve them.

Using Homebrew to install packages, you can search the package before installation:

brew search php

Above command will give you a list of available php packages.

Add a new repository for Homebrew:

brew tap homebrew/dupes
brew tap homebrew/php

You can use following command to check what options we can use with when installing the package:

brew options php70

Install Nginx on Mac

If you have installed Homebrew, installing Nginx on Mac is quite easy. Make sure you have installed homebrew. After that, please run following command:

brew install nginx

After installing successfully, you can find the Nginx in /usr/local/Cellar/nginx.

The configuration file of Nginx on Mac is located in /usr/local/etc/nginx/nginx.conf. You can edit it to configure your Nginx.

The default web root folder is /usr/local/Cellar/nginx/html. Actually, it is just a link to folder /usr/local/var/www.

Run following command to start Nginx:

sudo nginx

Run following command to reload Nginx:

sudo nginx -s reload

Run following command to stop Nginx:

sudo nginx -s stop

Enable your Nginx to handle PHP script, you need to change the server configuration file by adding following part:

        location ~ \.php$ {
            root           html;
            try_files      $uri $uri/ /index.php?$args;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

You can omit the root option if you want to use default server root option.

Install PHP on Mac

I am working on PHP language as well. If you don’t work with PHP, you can ignore this part. Here are the steps to install PHP on Mac with Homebrew.

brew tap homebrew/dupes
brew tap homebrew/php

Check available PHP package:

brew search php

Check available installation option:

brew options php70

Install PHP-FPM with brew:

brew install --without-apache --with-fpm --with-mysql php70

By default, PHP will read its configuration file in /etc folder. However, there is no php.ini and php-fpm.conf files in /etc. So you have to manually create them with following commands:

cd /etc
sudo cp php.ini.default ./php.ini
sudo cp php-fpm.conf.default php-fpm.conf

Next, I need to edit php-fpm.conf to make sure php-fpm can work on Mac. Please find follow lines and replace with given value:

pid = /usr/local/var/run/php/php-fpm.pid
error_log = /usr/local/var/log/php/php-fpm.log

Then, we have to create above files on Mac. After everything get ready, we can run this command to start php-fpm:

php-fpm

To stop the php-fpm, use this command:

sudo killall php-fpm

Sometimes, you may get this problem:

file not found

It could be something wrong in Nginx configuration file. I solve the problem by fix the fastcgi_param option.

Run PHP-FPM on Unix Socket
To burst the performance of PHP-FPM, we can run php-fpm on unix socket and let nginx pass php request to php-fpm socket. To do so, first edit the /etc/php-fpm.conf. Find the following line:

listen = 127.0.0.1:9000

Replace above statement with following line:

listen = /usr/local/var/run/php/php-fpm.sock

You may have question about where is my php-fpm.sock file. Lots of people search online about the pack of php-fpm socket file. Actually, you just set whatever path and sock file will be created automatically once you restart php-fpm.

Then, we are going to edit /usr/local/etc/nginx/nginx.conf file. Find the following line:

fastcgi_pass    127.0.0.1:9000;

Replace it with following line:

fastcgi_pass    unix:/usr/local/var/run/php/php-fpm.sock;

Install xdebug for PHP
On Mac, it’s quite simple to install xdebug for a certain PHP version. For example, I have installed PHP 7.0 on my Mac, then I use following command to install xdebug for PHP 7.0:

brew install php70-xdebug

Note: Checking Your php-fpm
This is a very important note. This problem takes me 2 days and I almost cannot find any information about the problem. Here is the problem.

I installed PHP 7.0 with php-fpm by Homebrew (brew) on my Mac. When I test my php version by php -v, I get the version of my PHP as following:

PHP 7.0.15 (cli) (built: Jan 22 2017 08:51:45) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Xdebug v2.5.1, Copyright (c) 2002-2017, by Derick Rethans

However, when I test my php-fpm version by php-fpm -v, or I am running phpinfo() in php page, I get the version like:

PHP 5.6.28 (fpm-fcgi) (built: Dec 6 2016 12:39:13)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

On my Mac, I have installed PHP 7.0 with xdebug, I can run the PHP without any problem. However, when I run the php-fpm, I get following error:

Failed loading /usr/local/opt/php70-xdebug/xdebug.so: dlopen(/usr/local/opt/php70-xdebug/xdebug.so, 9): Symbol not found: _zend_ce_error
Referenced from: /usr/local/opt/php70-xdebug/xdebug.so
Expected in: flat namespace
in /usr/local/opt/php70-xdebug/xdebug.so

When you are search this error message with these phrases “Failed loading xdebug“, “Failed loading dlopen xdebug.so“, “Symbol not found: _zend_ce_error“, “Expected in: flat namespace” on google, I cannot find any useful information. I truly believe it could be because my php and php-fpm version are different. So I am running following command to check where is the executable php and php-fpm.

which php

Here is what I get:

/usr/local/bin/php

And

which php-fpm

Here is what I get:

/usr/sbin/php-fpm

It’s strange, isn’t it? In the end, I found the problem and the solution. On my Mac, there is a pre-installed php-fpm in /usr/sbin folder. After I am running brew command to install the new php with fpm, all files are installed in /usr/local. Therefore, we need to make sure /usr/local/sbin is before /usr/sbin in our path. I also find a note on github.

Install MySQL on Mac

Installing MySQL on Mac is quite straight forward. Just running the Homebrew installation command:

brew install mysql

After installation, we can start the mysql server by:

mysql.server start

Now the MySQL is up. You can run the following command to make your database more secure:

mysql_secure_installation

Then you can setup everything basing on the prompt. For example, you can set the root password as root, though it is not recommended.

SSH

On Windows, I am using Putty to connect my remote Linux server. On Mac, the ssh client is a built-in command line app. It is very flexible to use it with Bash shell script. For example, I just wrote an bash script to connect remote Linux server without typing user name and password. If you don’t know what is bash shell script, it is like BAT file on Windows. Technically, Bash Shell is just one of several shells (zsh, ksh, tcsh, bash, or sh) available on Mac. You can also change other shell if you like. But I am just using the default Bash Shell. Here I will demonstrate how I create the auto login Bash Shell script for SSH.

First of all, create a script file and set it as executable.

touch sshlogin.expect.sh
chmod 755 sshlogin.expect.sh

Edit the file as following:

#!/usr/bin/expect -f
spawn ssh root@example.com -p 123456
expect "password: "
send "mypassword\r"
interact

After the script is ready, let’s run the script like this:

expect sshlogin.expect.sh

Note that the expect is a command line tool which helps to answer the questions programmely.

Besides using shell script, you can also use ssh key to login remote Linux server. First, create a key pair then copy the public key to remote Server. Here is the step:
Generate the key pair

ssh-keygen

Copy public key to remote server

ssh-copy-id -i ~/.ssh/id_rsa.pub username@example.com -p 12345

-p indicate to connect to 12345 port, it is not the password. Or you can use following command to manually copy the public key to remote Server:

scp ~/.ssh/id_rsa.pub username@example.com:~/.ssh/authorized_keys

Make sure that authorized_keys doesn’t exist. Otherwise, preceding code will overwrite the file which may overwrite other one’s public key. If the file exist, you can use this command:

cat ~/.ssh/id_rsa.pub | ssh username@example.com "cat - >> ~/.ssh/authorized_keys"

Make sure to backup the remote file before you do it.

Other Command Line Tips

I just mentioned command line is the most powerful tool on Mac. However it is very hard for Windows user to catch up. If you have any working experience on Linux, then it will not be a problem at all. Here are some small tricks which can help you to use command line better.

Show Current Shell

echo $SHELL

Show Path Environment Variable

echo $PATH

Change Path
On Mac, there are several ways to setup $PATH environment variable. Here I just give the example about changing path by .bash_profile if you are using bash shell.

cd ~
vi .bash_profile

Then, in the vi editor, please add following line:

export PATH="/usr/local/sbin:$PATH"

The above example will add “/usr/local/sbin” folder into your system path.

Open Finder on Current Path

open .

Folder Permission Setting in Mac
Different from Linux/Unix, Mac doesn’t have useradd/groupadd command. So I only use the UI to create the user “nginx” and group “nginx” for Nginx and PHP-FPM. Then I set the host website folder permission for this user and group.

sudo chmod -R 755 www
sudo chown -R nginx:nginx www
sudo umask 755 www

The 1st and 2nd commands are easy to understand. For the 3rd one, it means that all files and folders which are created later will have this permission.

Find Top CUP Usage App and Kill It
Mac provides a GUI widget “Activity Monitor” for you to check the CPU usage and memory usage. However, if there is an application which makes the system very slow, even unable to launch Activity Monitor, then how to find and kill the application to resume the system? Usually when the Mac is too slow to launch GUI application, it can still launch and run command line. Here is the command line to check the system resource usage and kill the application which make the system hang.

top -F -R -o cpu
sudo kill PID

Find Files by Name
Mac provides a very powerful command line to help you find files by name. Here is the example:

find /usr/local -name php-fpm

In preceding example, I am trying to find php-fpm file in folder /usr/local.

The first command will show you the top cpu consuming application, then you need to record the PID of the application. Next, use the second command to kill the application by given PID.

Summary

After this long article, here are some command line which will be used daily:

sudo nginx
sudo nginx -s reload
sudo nginx -s stop
sudo php-fpm
sudo killall php-fpm
mysql.server start
mysql.server stop

When you want to install some package with brew, these command line may help:

brew list ;search installed package
brew search app_name ;search app_name to be install
brew tap homebrew/php ;add new repository
brew options app_name ;search options along with app_name installation
brew install app_name ;install app_name
 

Leave a Reply

WordPress Themes

Free WordPress Themes