ubuntu 12.04 下 nginx php mysql 安裝筆記
本篇文章參考了 張宴 的《Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建勝過Apache十倍的Web伺服器(第6版)[原創]》,在其基礎上改造而來,對其表示感謝,因為我個人一直用的是ubuntu
注意:此安裝,是用來在自己的機器上開發環境用的,是以編譯時選項很多都打開了debug選項,要是做為生産環境的話,在編譯時請關掉debug選項,并加上優化參數
本文是 vincentzhwg 所寫,轉載請注明作者:vincentzhwg
系統版本:ubuntu desktop 64位
版本
nginx 1.0.14
php 5.4.0
mysql 5.5.22
先下載下傳好所有的檔案以便安裝,這裡下載下傳所有的檔案到 ~/Documents/LNMP/source 檔案夾中
建立檔案夾
mkdir -p ~/Documents/LNMP/source
mkdir -p ~/Documents/LNMP/extra
下載下傳相應的安裝源代碼包,需要下載下傳的包有以下幾種,我以自己下載下傳時的版本号為例
libiconv-1.14.tar.gz
libmcrypt-2.5.8.tar.gz
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.gz
pcre-8.30.tar.gz
mysql-5.5.22.tar.gz
nginx-1.0.14.tar.gz
php-5.4.0.tar.gz
檔案下載下傳可以在網上搜尋進入其官網後下載下傳,且壓縮格式不一定要跟我下載下傳的相同,就是解壓的時候根據不同的壓縮格式使用不同的指令
檔案也可以從我的CSDN資源空間上下載下傳,列出空間下載下傳位址清單如下:
http://download.csdn.net/detail/vincent17113/4179857
安裝基本編譯工具
apt-get install build-essential automake autoconf cmake
進入到存放這些源代碼包的檔案夾中
然後運作以下指令段做安裝前的準備
cd ~/Documents/LNMP/source/
tar zxvf libiconv-1.14.tar.gz -C ../extra/
cd ../extra/libiconv-1.14/
./configure --prefix=/usr/local
make
sudo make install
cd ../../source/
tar zxvf libmcrypt-2.5.8.tar.gz -C ../extra/
cd ../extra/libmcrypt-2.5.8/
./configure
make
sudo make install
sudo /sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
sudo make install
cd ../../../source/
tar zxvf mhash-0.9.9.9.tar.gz -C ../extra/
cd ../extra/mhash-0.9.9.9/
./configure
make
sudo make install
cd ../../source/
sudo /sbin/ldconfig
sudo ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
sudo ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
sudo ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
sudo ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
sudo ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
sudo ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
sudo ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
sudo ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
sudo /sbin/ldconfig
tar zxvf mcrypt-2.6.8.tar.gz -C ../extra/
cd ../extra/mcrypt-2.6.8/
./configure
make
sudo make install
cd ../../source/
tar zxvf pcre-8.30.tar.gz -C ../extra/
cd ../extra/pcre-8.30/
./configure
make
sudo make install
cd ../../source/
sudo /sbin/ldconfig
運作完以上指令,再運作下面的指令安裝上缺失的包
sudo apt-get install \
libcurses-ocaml-dev \
libxml2-dev \
libssl-dev \
libbz2-dev \
libcurl4-gnutls-dev \
libjpeg-dev \
libpng12-dev \
libxpm-dev \
libfreetype6-dev \
libxslt1-dev
運作以下指令産生軟連結
sudo ln -s /usr/lib/x86_64-linux-gnu/libXpm.a /usr/lib/libXpm.a
sudo ln -s /usr/lib/x86_64-linux-gnu/libXpm.so /usr/lib/libXpm.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libXpm.so.4 /usr/lib/libXpm.so.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libXpm.so.4.11.0 /usr/lib/libXpm.so.4.11.0
sudo /sbin/ldconfig
開始安裝 mysql
建立使用者與存放資料庫資料的檔案夾
sudo groupadd mysql
sudo useradd -r mysql -g mysql
sudo mkdir -p /home/mysql/3306/data
sudo chown -R mysql:mysql /home/mysql
解壓
cd ~/Documents/LNMP/source
tar zxvf mysql-5.5.22.tar.gz -C ../extra/
cd ../extra/mysql-5.5.22/
運作以下指令行編譯 mysql,比較長,記得編譯時是把下面的多行一起複制到指令視窗編譯
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLE_DEBUG_SYNC=1 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLED_PROFILING=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_LIBWRAP=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=yes
編譯完後運作下面兩個指令安裝
make
sudo make install
sudo cp /usr/local/mysql/support-files/my-medium.cnf /home/mysql/3306/my.cnf
編輯 my.cnf 檔案以支援 innodb,編輯完内容如下
# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /home/mysql/3306/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /home/mysql/3306/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
後期設定指令
cd /usr/local/mysql
chown -R mysql:mysql .
初始化資料庫
sudo /usr/local/mysql/scripts/mysql_install_db --defaults-file=/home/mysql/3306/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/3306/data
啟動資料庫
sudo /usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/3306/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/3306/data &
修改使用者名密碼,其中的 NEW_PASSWORD 換成你自己想要設定的密碼
/usr/local/mysql/bin/mysqladmin -u root password 'YOUR PASSWORD'
關閉資料庫使用如下指令
/usr/local/mysql/bin/mysqladmin -u root -p shutdown
安裝 PHP
建立 www 使用者
sudo groupadd www
sudo useradd -r www -g www
解壓
cd ~/Documents/LNMP/source/
tar zxvf php-5.4.0.tar.gz -C ../extra/
cd ../extra/php-5.4.0/
編譯指令,有些長,記得一起複制到終端中
./configure \
--prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo-mysql=/usr/local/mysql \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl \
--with-pear \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-xpm-dir \
--with-freetype-dir \
--with-mcrypt \
--with-mhash \
--with-openssl \
--with-xmlrpc \
--with-xsl \
--with-bz2 \
--with-gettext \
--disable-debug \
--enable-exif \
--enable-wddx \
--enable-zip \
--enable-bcmath \
--enable-calendar \
--enable-ftp \
--enable-mbstring \
--enable-soap \
--enable-sockets \
--enable-shmop \
--enable-dba \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg
編譯完成後,進行安裝
make ZEND_EXTRA_LIBS='-liconv'
sudo make install
設定配置檔案
sudo cp ~/Documents/LNMP/extra/php-5.4.0/php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/etc
sudo cp php-fpm.conf.default php-fpm.conf
修改配置檔案
修改 php.ini ,去掉 date.timezone 的注釋,并設定為 date.timezone = PRC
更改權限
sudo chown -R www:www /usr/local/php
啟動 php-fpm
sudo /usr/local/php/sbin/php-fpm
關閉的話,使用以下指令查找出程序号
sudo ps -x | grep php-fpm
假設查找到的程序号為 1214 ,則運作以下指令關閉 php-fpm 進行
sudo kill -QUIT 1214
安裝 nginx
解壓
cd ~/Documents/LNMP/source/
tar zxvf nginx-1.0.14.tar.gz -C ../extra/
cd ../extra/nginx-1.0.14/
編譯
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module
安裝
make
sudo make install
編輯 nginx.conf
将第二行的 #user 去掉注釋,并改為
user www www;
啟動nginx
sudo /usr/local/nginx/sbin/nginx
關閉
sudo /usr/local/nginx/sbin/nginx -s stop
重載配置檔案
sudo /usr/local/nginx/sbin/nginx -s reload
至此整個安裝過程結束,做個測試吧
sudo mkdir -p /home/host/php/test
sudo mkdir /usr/local/nginx/conf/servers
在 /home/host/php/test下建立一檔案 index.php ,内容如下
<?php
phpinfo();
在 /usr/local/nginx/conf/servers 下建立一檔案 test.vhost,内容如下
server {
listen 80;
server_name vct.test.com;
root /home/host/php/test;
location / {
index index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
編輯 /usr/local/nginx/conf/nginx.conf 檔案,在最後一個 } 之前加入一行
include servers/*.vhost;
編輯 /etc/hosts 檔案,加入一行
127.0.0.1 vct.test.com
運作以下指令,更改權限并啟動服務
sudo chown -R www:www /usr/local/nginx
sudo chown -R nobody:nogroup /home/host/php
sudo /usr/local/php/sbin/php-fpm
sudo /usr/local/nginx/sbin/nginx
然後,打開浏覽器,輸入位址 vct.taiup.com ,應該就能看到 php 的相關資訊了。
為了友善,把三樣服務加入到開機自啟動中,這樣以後一開機就會自動啟動了,也可以容易避開中途啟動時端口被占用的問題
編輯 /etc/rc.local 檔案,在 exit 0 語句前加入以下語句
/usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/3306/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/3306/data &
sleep 5
/usr/local/php/sbin/php-fpm
sleep 3
/usr/local/nginx/sbin/nginx
sleep 3