Nginx mysql php mac

johnantoni / gist:07df65898456ace4307d5bb6cbdc7f51

This is my take on how to get up and running with NGINX, PHP-FPM, MySQL and phpMyAdmin on OSX Yosemite.

This article is adapted from the original by Jonas Friedmann. Who I just discovered is from Würzburg in Germany. A stonesthrow from where I was born 😉

Make sure you have the latest version of XCode installed. Available from the Mac App Store.

Install the Xcode Command Line Tools:

Homebrew is the missing package manager for OSX.

Download and install using the following command:

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

Check for any problems or conflicts:

Update and Upgrade brew formulas:

brew update && brew upgrade 

We will need to add some extra php formulas to brew so that we can install PHP and PHP-FPM:

brew tap homebrew/dupes brew tap josegonzalez/homebrew-php 

tip: to uninstall a tap do

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

This will compile PHP on your machine and may take a few minutes.

Make sure you use the version number that got installed previously:

mkdir -p ~/Library/LaunchAgents cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/ 
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist 

Check that PHP-FPM is listening on port 9000:

lsof -Pni4 | grep LISTEN | grep php 
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents 

And start the database server:

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

Since we want to use port 80 have to start the Nginx process as root:

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

Start Nginx for the first time:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

The default configuration is set that it will listen on port 8080 instead of the HTTP standard 80. Ignore that for now:

curl -IL http://localhost:8080 

The output should look like:

HTTP/1.1 200 OK Server: nginx/1.6.0 Date: Tue, 08 Jul 2014 21:40:38 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 08 Jul 2014 21:35:25 GMT Connection: keep-alive ETag: "53bc641d-264" Accept-Ranges: bytes 
sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

Create some folders which we are going to use in the configuration files:

mkdir -p /usr/local/etc/nginx/logs mkdir -p /usr/local/etc/nginx/sites-available mkdir -p /usr/local/etc/nginx/sites-enabled mkdir -p /usr/local/etc/nginx/conf.d mkdir -p /usr/local/etc/nginx/ssl sudo mkdir -p /var/www sudo chown :staff /var/www sudo chmod 775 /var/www 

Remove the current default nginx.conf (also available as /usr/local/etc/nginx/nginx.conf.default in case you want to take a look) and download this custom one via curl from GitHub:

rm /usr/local/etc/nginx/nginx.conf curl -L https://gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf 

Download the following PHP-FPM configuration from GitHub:

curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm 

Create default virtual hosts

curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin 

Clone my example virtual host (including 404, 403, and phpinfo() rewrite) using git

git clone https://github.com/gil0mendes/nginx-virtual-host.git /var/www rm -rf /var/www/.git 

Create folder for our SSL certificates and private keys:

mkdir -p /usr/local/etc/nginx/ssl 

Generate 4096bit RSA keys and the self-sign the certificates in one command:

openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj '/C=US/ST=State/L=Town/O=Office/CN=localhost' -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj '/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin' -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt 

Now we need to symlink the virtual hosts that we want to enable into the sites-enabled folder:

ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin 
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

Thats it, everything should be up and running. Click on the links below to ensure that:

  • http://localhost → “Nginx works” page
  • http://localhost/info → phpinfo()
  • http://localhost/nope → ” Not Found” page
  • https://localhost:443 → “Nginx works” page (SSL)
  • https://localhost:443/info → phpinfo() (SSL)
  • https://localhost:443/nope → “Not Found” page (SSL)
  • https://localhost:306 → phpMyAdmin (SSL)

Control your services like a boss

Because you probably need to restart the one or other service sooner or later, you probably want to set up some alias. Download aliases:

curl -L https://gist.githubusercontent.com/mgmilcher/c3a1d0138dde3eb0f429/raw/ed04e90d7770dbb62c60e1e4a912f75adc46cb5e/osx-server-aliases -o /tmp/.aliases 

Close and open the terminal or type source ~/.profile or source ~/.zshrc to reload the profile.

The following commands are now available

nginx.start nginx.stop nginx.restart 
php-fpm.start php-fpm.stop php-fpm.restart 
mysql.start mysql.stop mysql.restart 
nginx.logs.error nginx.logs.access nginx.logs.default.access nginx.logs.default-ssl.access nginx.logs.phpmyadmin.access 

Hi Jonathan, can you help me to understand on how to add multiple website in var/www/ for instance I have site1 site 2 inside www. because here we changed the nginx.conf . thank you

Источник

macOS: Install nginx, MySQL and PHP via brew

Since I was not satisfied with the performance of MAMP PRO and also used my local development environment mainly within the scope of localhost and not within multiple hosts, I decided to install a combination of nginx, MySQL and PHP via brew.

brew is a package manager, which allows to install many packages without a hassle.

Following I show you the fast installation of the packages and their basic configuration on macOS.

nginx

You can start the installation via the following command in your Terminal:

brew install nginxCode language: Bash (bash)

The initial configuration can be found in the directory /usr/local/etc/nginx (or in /opt/homebrew/etc/nginx for Macs with Apple Silicon). At first, edit the nginx.conf in this directory, e.g. with this command:

pico /usr/local/etc/nginx/nginx.confCode language: Bash (bash)

To use PHP later on you need to adjust this block:

 location / < root html; index index.html index.htm; >Code language: Nginx (nginx)

So that it looks like this:

 root /path/to/webroot; index index.html index.htm index.php; location / < autoindex on; try_files $uri $uri/ /index.php?$args; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; >Code language: Nginx (nginx)

You need to set the path to your web root in the first line, which will be displayed after opening http://localhost in your browser. The line starting with index defines, which files will be accessed directly while opening a directory. Here you need to add the index.php at the end.

autoindex on; allows you to display the directory tree if there is no file specified in index . This is helpful for development, especially if you have multiply projects in sub-directories. Please never enable this function in a production environment.

The try_files directive declares how nginx tries to access a requested file. The given variant should fit the need of most applications. If you need a different, it should be found in the documentation of the particular application.

The different proxy buffer settings fix some issues with bigger websites (e.g. if they send large headers).

To be able to user PHP later on, you also need to add the following block below the one from above:

 location ~ \.php$ < fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; >Code language: Nginx (nginx)

By default, nginx runs on port 8080 since port 80 and 443, which are usually used for HTTP and HTTPS, can only be used with administrator permissions on macOS. If you want to use port 80, you need to adjust the following line:

Afterwards, you can start nginx with the following command:

brew services run nginxCode language: Bash (bash)

Or as administrator via sudo :

sudo brew services run nginxCode language: Bash (bash)

If you want to start the nginx server on system boot, you can use the following command instead:

brew services start nginxCode language: Bash (bash)

Note: If you need to start nginx with administrator permissions to use port 80, it’s not possible to automatically start it on system boot.

To stop the server, you can use the following command:

brew services stop nginxCode language: Bash (bash)

And to restart it, use the restart command:

brew services restart nginxCode language: Bash (bash)

This command chain can be used for all services of brew, so also for MySQL and PHP (you just need to replace the nginx at the end to mysql or php ).

MySQL

To install MySQL, you can use the following command in your Terminal:

brew install mysqlCode language: Bash (bash)

The configuration file can be found in /usr/local/etc/my.cnf (or in /opt/homebrew/etc/my.cnf for Macs with Apple Silicon) and can be opened similar to the nginx configuration:

pico /usr/local/etc/my.cnfCode language: Bash (bash)

As basis you should use at least the following configuration:

# Default Homebrew MySQL server config [mysqld] # Only allow connections from localhost bind-address = 127.0.0.1 mysqlx-bind-address = 127.0.0.1 socket = /tmp/mysql.sock character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci character-set-client-handshake = falseCode language: PHP (php)

If the service is already running after the installation, you should restart it now:

brew services restart mysqlCode language: Bash (bash)

To test if the MySQL server is running, you can enter the following command:

The default password for user root is also root . I leave that value in my local test environment.

If everything works, the output should look like the following:

$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 39 Server version: 8.0.25 Homebrew Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>Code language: JavaScript (javascript)

PHP

Even PHP can be installed via a single command in the Terminal:

brew install phpCode language: Bash (bash)

Currently, PHP 8.0 will be installed using this command. If you want to install another version, you can do it like this:

brew install php@7.4Code language: Bash (bash)

This way, PHP 7.4 will also be installed if you already used the first command.

Similar to Linux, every PHP version has its own configuration directory in /usr/local/etc/php (or in /opt/homebrew/etc/php for Macs with Apple Silicon):

ls -l /usr/local/etc/php total 0 drwxr-xr-x 10 matze admin 320 Jul 3 15:55 7.4 drwxr-xr-x 10 matze admin 320 Jul 1 18:23 8.0

The php.ini can be found in the directory of every version, e.g. in /usr/local/etc/php/8.0/php.ini .

Switch between PHP versions

To switch between PHP versions, you can use the unlink and link commands of brew.

To switch to PHP 7.4, use the following commands:

brew unlink php brew link php@7.4 --force --overwriteCode language: Bash (bash)

To switch back, just replace the values of php and php@7.4 in the commands:

brew unlink php@7.4 brew link php --force --overwriteCode language: Bash (bash)

Before doing that, please make sure to stop any current running PHP version via brew services stop php or brew services stop php@7.4 .

Afterwards, restart your Terminal session to apply the changes.

Источник

Читайте также:  Что такое update в питоне
Оцените статью