Lnmp(linux+ nginx+mysql+php) web服务器环境搭建详细步骤:
- 首先进入linux系统,因为操作都在终端进行,故先安装vim插件,方便后续操作。
Apt-get install vim 完成安装。
- 准备软件包
Autoconf-2.61.tar.gz mcrypt-2.6.7.tar.gz
Eaccelerator -0.9.6.1.tar.gz(加速器) memcache -2.2.3.tar.gz
mhash-0.9.9.tar.gz freetype -2.3.5.tar.gz
mysql-5.5.14.tar.gz gd-2.0.35.tar.gz
Imagick -2.2.2.tar.gz nginx -0.7.21.tar.gz
ImageMagick -6.7.5-0.tar.gz jpeg -6b.tar.gz
Libiconv -1.12.tar.gz pcre -8.32.tar.gz
PDO_mysql -1.0.2.tar.gz libmcrypt -2.5.8.tar.gz
Php -5.2.6 –fpm -0.5.9.diff.gz php-5.3.19.tar.gz
Libpng -1.2.31.tar.gz zlib -1.2.7.tar.gz
Libxml2 -2.6.30.tar.gz
- 检查安装使用的编译工具Gcc是否存在。
Gcc –v
Apt-get install gcc
Apt-get install g++
Apt-get install make
- 安装PHP之前,应先安装PHP5需要的最新版本库文件,例如libxml2、libmcrypt以及GD2库等文件。安装GD2库是为了让PHP5支持GIF、PNG和JPEG图片格式,所以在安装GD2库之前还要先安装最新的zlib、libpng、freetype和jpegsrc等库文件
1). 安装libxml2最新库文件
p ./configure --prefix=/usr/local/libxml2
p # make
p # make install
2). 安装libmcrypt最新库文件
p ./configure --prefix=/usr/local/libmcrypt
p # make
p # make install
3). 安装zlib最新库文件
p #./configure
p # make
p # make install
4) .安装libpng最新库文件
p # ./configure --prefix=/usr/local/libpng
p # make
p # make install
5). 安装jpeg6最新库文件
首先看有没有安装libtool 及 libtool-ltdl-devel
rpm -qa | grep libtool
然后进入jpeg-6b的源码目录,然后执行以下步骤,切记!COPY到当前目录注意后面的点(.)
cp
/usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
也就是把 libtool里面的两个配置文件拿来覆盖掉jpeg-6b目录下的对应文件
make clean 再重新configure
没有权限的时候先建立对应的文件夹,再次make
install就行了
p #mkdir /usr/local/jpeg6 //建立jpeg6软件安装目录
p # mkdir /usr/local/jpeg6/bin //建立存放命令的目录
p # mkdir /usr/local/jpeg6/lib //创建jpeg6库文件所在目录
p # mkdir /usr/local/jpeg6/include //建立存放头文件目录
p # mkdir -p /usr/local/jpeg6/man/man1 //建立存放手册的目录
p # ./configure \
p > --prefix=/usr/local/jpeg6/ \
p > --enable-shared \ //建立共享库使用的GNU的libtool
p > --enable-static //建立静态库使用的GNU的libtool
p Make && make install
6). 安装freetype最新库文件
p # ./configure --prefix=/usr/local/freetype
p # make
p # make install
7). 安装autoconf最新的库文件
p # ./configure
p # make
p # make install
8). 安装最新的GD库文件
p #./configure \ //配置命令
p > --prefix=/usr/local/gd2/ \ //指定软件安装的位置
p > --with-zlib=/usr/local/zlib/ \ //指定到哪去找zlib库文件的位置
p > --with-jpeg=/usr/local/jpeg6/ \ //指定到哪去找jpeg库文件的位置
p > --with-png=/usr/local/libpng/ \ //指定到哪去找png库文件的位置
p > --with-freetype=/usr/local/freetype/ //指定到哪去找freetype 2.x字体库的位置
p Make && make install
注意:可能出现错误
make[2]: *** [gd_png.lo] Error 1
make[2]: Leaving directory `/tmp/gd-2.0.26gif'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/tmp/gd-2.0.26gif'
make: *** [all] Error 2
解决方法:vi gd_png.c 找到“png.h”改成“/usr/local/libpng/include/png.h”
- 安装PHP 5.2.6(FastCGI模式)
1). 编译安装PHP 5.2.6所需的支持库:
tar -zxvf libiconv-1.12.tar.gz
cd libiconv-1.12/
./configure --prefix=/usr/local
make
make install
2). 安装libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd http://www.cnblogs.com/
3). 安装mhash-0.9.9
./configure
make
make install
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la #创建符号链接
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s/usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
4). 安装mcrypt-2.6.7
/sbin/ldconfig
./configure
make
make install
- 编译安装MySQL(注:mysql5.5以后版本需要用cmake编译)
1).先安装cmake
# wget
http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz
# tar zxvf cmake-2.8.4.tar.gz
# cd cmake-2.8.4
#.configure
# make && make install
2).创建mysql安装目录
#mkdir -p /usr/local/webserver/mysql/
3).创建数据存放目录
#mkdir –p /data/mysql/
4).创建用户和用户组与赋予数据存放目录权限
#groupadd mysql
#useradd –g mysql mysql
#chown mysql.mysql –R /data/mysql/
5) .编译mysql-5.5
先安装一些必须的软件apt-get
install
cmake automake autoconf bison
apt-get
install
libncurses5-dev
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql/ \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_TCP_PORT=3306 \
-DEXTRA_CHARSETS=all \-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0
[出现警告:The variable, 'MYSQL USER', specified manually, was not used during the generation. ,可以忽略]
Make && make install
6).复制配置文件
# cp support-files/my-medium.cnf /etc/my.cnf
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
7).初始化数据库
bash scripts/mysql_install_db --user=mysql --basedir=/usr/local/webserver/mysql --datadir=/data/mysql/
8).启动mysql服务
# /etc/init.d/mysqld start 或者service mysqld start
Ps –ef | grep mysql 查看是否启动
/usr/local/webserver/mysql/bin/mysql –u root –p 登录数据库
9). 开启远程登录
One: 改表法
可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改“mysql” 数据库里的 “user” 表里的 “host” 项,从“localhost”改称“%”
x:\>mysql -u root -pvmware
mysql> use mysql;
mysql> update user set host = ‘%’ where user = ‘root’;
mysql> select host, user from user;
mysql> flush privileges;
Two:授权法
例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
Mysql> grant all privileges on *.* to ‘myuser’@’%’identified by ‘mypassword’ with grant option;
- 编译安装PHP(FastCGI模式)
可能出现错误:error: Cannot find OpenSSL’s <evp.h>
Solutions:apt-get install libcurl3-openssl-dev
出现
configure: error: sasl.h not found!
安装
apt-get install libsasl2-dev
|
tar zxvf php-5.3.19.tar.gz
gzip -cd php-5.2.6-fpm-0.5.9.diff.gz | patch -d php-5.2.6 -p1
cd php-5.2.6/
./configure
--prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir=/usr/local/freetype --with-jpeg-dir=/usr/local/jpeg6
--with-png-dir=/usr/local/libpng
--with-zlib --with-libxml-dir=/usr/local/libxml2 --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd-dir=/usr/local/gd2 --enable-gd-native-ttf --with-openssl –with-mhash
--enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc
--enable-zip –enable-soap –without-pear
Make ZEND_EXTRA_LIBS=’-liconv’
make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../
1). 编译安装php5扩展模块
[memcache]
tar zxvf memcache-2.2.3.tgz
cd memcache-2.2.3/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
[eaccelerator加速器]
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3/
/usr/local/webserver/php/bin/phpize
./configure
--enable-eaccelerator=shared
--with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
[PDO_MYSQL]
cd PDO_mysql-1.0.2/
./configure –with-php-config=/usr/localwebserver/php/bin/php-config
--with-pdo-mysql=/usr/local/webserver/mysql
Make
Make install
[ImageMagick-6.5.1-2]
cd ImageMagick-6.5.1-2/
./configure
make && make install
[imagick-2.2.2]
cd imagick-2.2.2/
/usr/local/webserver/php/bin/phpize
./configure –with-php-config=/usr/local/webserver/php/bin/php-config
Make && make install
2).修改php.ini 文件
查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = “pdo_mysql.so”
extension = “imagick.so”
3). 配置eAccelerator加速PHP:
mkdir -p /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="128"
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="300"
eaccelerator.shm_prune_period="120"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
修改配置文件:
vi /etc/sysctl.conf
输入以下内容:
kernel.shmmax = 134217728
不执行上面操作可能出现下面错误
Starting php_fpm eAccelerator: Could not allocate 67108864 bytes, the maximum size the kernel allows is 33554432 bytes. Lower the amount of memory request or increase the limit in /proc/sys/kernel/shmmax. PHP Warning: [eAccelerator] Can not create shared memory area in Unknown on line 0 PHP Fatal error: Unable to start eAccelerator module in Unknown on line 0 failed
然后执行以下命令使配置生效:
/sbin/sysctl -p
4). 创建www用户和组
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /data0/htdocs/www
chmod +w /data0/htdocs/www
chown -R www:www /data0/htdocs/www
5).创建php-fpm配置文件
cp /usr/local/webserver/php/etc/php-fpm.conf.default php-fpm.conf
配置参数如下:
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s
rlimit_files = 1048576
[www]
user = nobody
group = nogroup
listen = /tmp/php-cgi.sock
pm = ondemand
pm.max_children = 100
pm.process_idle_timeout = 5s
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
slowlog = /var/log/slow.log
request_slowlog_timeout = 5s
request_terminate_timeout = 10s
rlimit_files = 10240
注意红色部分:有可能出现502 bad gateway 错误
6).启动php-cgi进程
ulimit –SHn 65535
/usr/local/webserver/php/sbin/php-fpm start
注:php5.3以前的版本 usr/local/webserver/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload
- 安装Nginx 0.7.21
1).安装nginx所需的pcre库:
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure
make && make install
cd ../
2). 安装nginx
tar zxvf nginx-0.7.21.tar.gz
cd nginx-0.7.21/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
3).创建nginx日志目录
mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs
4).创建nginx配置文件
cp /usr/local/webserver/nginx/conf/nginx.conf.default nginx.conf
配置如下:
user www www;
worker_processes 8;
error_log /data1/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name demo.test.com;
index index.html index.htm index.php;
root /data0/htdocs/www;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock; 注意此配置项
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/access.log access;
}
}
5).在/usr/local/webserver/nginx/conf/ 目录下创建fcgi.conf文件
Vi fcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
6).启动nginx
Ulimit –SHn 51200
/usr/local/webserver/nginx/sbin/nginx
- 配置开机自动启动nginx+php
Vi /etc/rc.local
在末尾增加以下内容
Ulimit –SHn 51200
/usr/local/webserver/php/sbin/php-fpm
/usr/local/webserver/nginx/sbin/nginx
10. 优化linux 内核参数
vi /etc/sysctl.conf
在末尾增加以下内容
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
使配置立即生效:
/sbin/sysctl -p
11. 在不停止nginx服务的情况下平滑变更nginx配置
1、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/webserver/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully
2、这时,输入以下命令查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302
或者无需这么麻烦,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/webserver/nginx/nginx.pid`
12. 编写每天定时切割nginx日子的脚本
1、创建脚本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
输入以下内容:
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
2、设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
输入以下内容:
00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh