Help us understand the problem. What is going on with this article?

phpでmecabを使う手順

More than 5 years have passed since last update.

mecabをlinuxにインストール

sudo yum install gcc-c++
cd /tmp
wget https://mecab.googlecode.com/files/mecab-0.996.tar.gz
tar zxfv mecab-0.996.tar.gz
cd mecab-0.996
sudo ./configure --enable-utf8-only
sudo make
sudo make install

辞書のインストール

wget https://mecab.googlecode.com/files/mecab-ipadic-2.7.0-20070801.tar.gz
tar zxfv mecab-ipadic-2.7.0-20070801.tar.gz
cd mecab-ipadic-2.7.0-20070801
sudo ./configure --with-charset=utf8
sudo make
sudo make install

動作確認

mecab
すもももももももものうち

下記のようになっていればOK。

すもも  名詞,一般,*,*,*,*,すもも,スモモ,スモモ
も      助詞,係助詞,*,*,*,*,も,モ,モ
もも    名詞,一般,*,*,*,*,もも,モモ,モモ
も      助詞,係助詞,*,*,*,*,も,モ,モ
もも    名詞,一般,*,*,*,*,もも,モモ,モモ
の      助詞,連体化,*,*,*,*,の,ノ,ノ
うち    名詞,非自立,副詞可能,*,*,*,うち,ウチ,ウチ

phpにmecabモジュールを入れる

  • php-mecabのインストール
cd /tmp
wget https://github.com/rsky/php-mecab/archive/master.zip
unzip master.zip
cd php-mecab-master/mecab
phpize
sudo ./configure
sudo ./configure --with-php-config=/usr/bin/php-config --with-mecab=/usr/local/bin/mecab-config
sudo make
sudo make install
  • シンボリックリンクの作成
sudo ln -s /usr/lib64/php/modules/mecab.so /etc/php.d
  • php.iniの編集
vim /etc/php.ini
/etc/php.ini
 ;;;;;;;;;;;;;;;;;;;;;;
 ; Dynamic Extensions ;
 ;;;;;;;;;;;;;;;;;;;;;;

 ; If you wish to have an extension loaded automatically, use the following
 ; syntax:
 ;
 ;   extension=modulename.extension
 ;
 ; For example, on Windows:
 ;
 ;   extension=msql.dll
 ;
 ; ... or under UNIX:
 ;
 ;   extension=msql.so
 ;
 ; ... or with a path:
 ;
 ;   extension=/path/to/extension/msql.so
 ;
 ; If you only provide the name of the extension, PHP will look for it in its
 ; default extension directory.

 ;;;;
 ; Note: packaged extension modules are now loaded via the .ini files
 ; found in the directory /etc/php.d; these are loaded by default.
 ;;;;

 extension=mecab.so

  • apacheの再起動
service httpd restart
  • 動作確認
php -r 'phpinfo();' | grep 'mecab' -i 

以下のようになっていれば問題ない。

mecab
MeCab Support => enabled
MeCab Library => 0.996 => 0.996
mecab.default_dicdir => no value => no value
mecab.default_rcfile => no value => no value
mecab.default_userdic => no value => no value
Keech
難しいことをわかりやすく伝えることを心がけています。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away