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