LoginSignup
10
11

More than 5 years have passed since last update.

Macでローカルウェブを設定

Last updated at Posted at 2014-10-02

自分用のメモなのでわかりにくいかも...

目標と目的

ローカル環境でapacheを使いたいのでいろいろしていただが、案外ややこしかったので記録しておいた。wordpressを使うこともあったので最低限apache+php+mysqlは動くようにしておく。

httpd.confなどをいじるときはバックアップとっておきましょう.

/etc/apache2/original/ に初期設定があるので最悪これで初期化出来る。

設定ファイルは /private/etc/apache2/ にある。これが /etc/apache2/httpd.conf に読み込まれているようなのでいじるならprivateの方をいじったほうが良さそう。ただしhttpd.confに関しては/etc/apache2/に方がいいのかな?

usrごとの設定はhttpd-userdir.confとapache2/user/.confで行っている。

読み込み順は
etc/apache2/httpd.conf < etc/apache2/httpd-userdir < /private/etc/apache2/user/
.conf

Virtual hosts設定

apache virtualホスト設定

  • '+'は追加
  • '-'は削除
  • '='は変更

httpd.conf

= Include /private/etc/apache2/extra/httpd-vhosts.conf

/private/etc/apache2/extra/httpd-vhosts.confに

+
<VirtualHost *:80>
  DocumentRoot "/Library/WebServer/Documents/testsite"
  ServerName localsite.com
  ErrorLog "/private/var/log/apache2/testsite_log"
  CustomLog "/private/var/log/apache2/testsite_log" common
</VirtualHost>

/etc/hosts

+ 127.0.0.1 localsite.com

apache再起動

% sudo apachectl restart

エラーログは以下のようにしてみることが出来るので上手くいかなかった場合参照しよう。

% tail /private/var/log/apache2/testsite_log

リンクを貼る

実態は/project/testsiteであるとして/Library/WebServer/Documents/にシンボリックリンクを作る

% sudo ln -s ~/project/testsite /Library/WebServer/Documents

これは主に以下の理由からapacheがWebServerしか見えないようにするためにした。

  • apacheがhomeディレクトリにアクセスできない
  • homeディレクトリのパーミッションを変えたくない

他にもいい方法があるかもしれない。あれば教えて下さい。

サイトを増やす場合はこのvirtualホストの手順を繰り返せば良い。

php インストール

ここを参考に行った

php55のオプションを確かめる。

% brew info php55
==> Options
........ 

とりあえずインストール

% brew install php55 --with-mysql

なにやらエラーがでた。

% brew install php55 --with-mysql
==> Downloading http://www.php.net/get/php-5.5.17.tar.bz2/from/this/mirror
Already downloaded: /Library/Caches/Homebrew/php55-5.5.17
==> ./configure --prefix=/usr/local/Cellar/php55/5.5.17 --localstatedir=/usr/local/var --sysconfdir=/usr/lo
checking for png_write_image in -lpng... yes
If configure fails try --with-xpm-dir=<DIR>
checking for fabsf... yes
checking for floorf... yes
configure: error: GD build test failed. Please check the config.log for details.

READ THIS: https://github.com/Homebrew/homebrew/wiki/troubleshooting
If reporting this issue please do so at (not Homebrew/homebrew):
  https://github.com/josegonzalez/homebrew-php/issues

ここを参考に試してみたところ上手くいった。

% brew rm freetype jpeg libpng gd
% brew install freetype jpeg libpng gd
brew install php55 --with-mysql
....
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php5_module    /usr/local/opt/php55/libexec/apache2/libphp5.so

The php.ini file can be found in:
    /usr/local/etc/php/5.5/php.ini

✩✩✩✩ PEAR ✩✩✩✩

If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
    chmod -R ug+w /usr/local/Cellar/php55/5.5.17/lib/php
    pear config-set php_ini /usr/local/etc/php/5.5/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

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

PHP55 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file:

      export PATH="$(brew --prefix homebrew/php/php55)/bin:$PATH"

To have launchd start php55 at login:
    ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents
Then to load php55 now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
==> Summary
 /usr/local/Cellar/php55/5.5.17: 493 files, 40M, built in 6.5 minutes

PEARやExtensionsやCLIを入れるなら上記を参考にするといいだろう。

htppd.confにphp55の読み込みを追加

+ LoadModule php5_module /usr/local/opt/php55/libexec/apache2/libphp5.so

その後apache再起動。

これはphpのバージョンアップごとにしなければならない模様。

php.iniの設定(よく分からんからデフォ)

/usr/local/etc/php/5.5/php.ini

[Date]
date.timezone = Asia/Tokyo

[mbstring]
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.detect_order = auto

こんなのもあったよ

extention info
Zend OPcache コンパイル後のコードをメモリに保存して再利用することでパフォーマンスを向上させる仕組み
APCu ユーザーキャッシュ機能

mysqlのインストール

ここを参考に進める

brew install mysql

.....

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
    mysql -uroot

To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
    mysql.server start
==> Summary
?  /usr/local/Cellar/mysql/5.6.20_1: 9578 files, 338M

mysql起動と停止

とりあえずのところは手動で起動するようにしておく。(そこそこメモリを食うため)

mysql.server start
mysql.server stop

rootパスワードを変更する。

% mysqladmin -u root password 'toor'

MySQLのセキュリティ設定

% mysql_secure_installation

以下が設定される。

  1. rootパスワードの設定
  2. anonymosユーザーの排除
  3. rootユーザーのログインをlocalhostに制限
  4. testデータベースの削除
  5. privilegeテーブルのリロード

my.cnfを設置する。

my.cnfの読み込み順番と設置

% mysql --help | grep my.cnf

/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf

mysqlはbrewで入れているので近い/usr/local/etc/my.cnfにmy.cnfを設置する。

% touch /usr/local/etc/my.cnf

my.cnfの設定

開発用なので、あまりデータ量も多くなくメモリを使用したくない。
とりあえず丸パクリ。ここを参照すれば説明があると思う
400mbから90mbまでメモリ使用量がへった。

[client]
port = 3306
socket = /tmp/mysql.sock

[mysql]
show-warnings
#safe-updates
prompt = '[\d] mysql> '

[mysqld]
# basic
datadir = /usr/local/var/mysql
#tmpdir = /tmp
port = 3306
socket = /tmp/mysql.sock
character-set-server = utf8
default-storage-engine = InnoDB
explicit_defaults_for_timestamp = 1
lower-case-table-names = 2

# max connections
max_connections = 64
# table_open_cache = max_connections * tables used in one transaction + 5
table_open_cache = 800
# table_definition_cache = all tables(50) + max_connections + 5
table_definition_cache = 400
# open_files_limit = table_open_cache * 1.4
open_files_limit = 1120

# global buffer
key_buffer_size = 16M
query_cache_type = 0
# innodb_buffer_pool_size = available memory for Mysql * 0.7
innodb_buffer_pool_size = 300M

# thread buffer
read_buffer_size = 256K
read_rnd_buffer_size = 512K
join_buffer_size = 256K
sort_buffer_size = 512K

# InnoDB
innodb_file_per_table
innodb_autoextend_increment = 64
innodb_log_files_in_group = 2
#innodb_log_group_home_dir = /tmp
innodb_log_file_size = 128M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_flush_neighbors=0
#innodb_flush_method=O_DIRECT
innodb_thread_concurrency = 4
innodb_commit_concurrency = 4

# log
#log_output = /tmp
general_log = 0
slow_query_log = 0

[mysqldump]
#max_allowed_packet = 16M
quick
set-charset
single-transaction

参考

apache virtualホスト設定

10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11