LNMP----初步搭建

  1. 云栖社区>
  2. 博客>
  3. 正文

LNMP----初步搭建

技术小阿哥 2017-11-27 11:22:00 浏览1033
展开阅读全文

1.Nginx 安装配置

# mkdir -p /app/3rd

# cd /app/3rd

安装pcre也可以yuminstall pcre pcre-devel -y

# wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.tar.gz

# tar xf pcre-8.30.tar.gz

# cd pcre-8.30

# ./configure

# make && makeinstall

安装openssl

# cd ../

# yum install  openssl openssl-devel -y

安装nginx

# useradd -s /sbin/nologin -M zpy

# wget -q http://nginx.org/download/nginx-1.6.2.tar.gz

# tar xf nginx-1.6.2.tar.gz

# cd nginx-1.6.2

# ./configure  --user=zpy --group=zpy --prefix=/app/zpy/nginx1.6.2 --with-http_stub_status_module  --with-http_ssl_module--with-pcre=/app/3rd/pcre-8.30

此处如果pcre为yum安装,则不需要指定pcre的源码包路径,反之指定路径

# make && makeinstall

# echo $?

0

# ln -s/app/zpy/nginx1.6.2/ /app/zpy/nginx

#/app/zpy/nginx/sbin/nginx -t

#/app/zpy/nginx/sbin/nginx -s reload

# lsof -i:80

COMMAND  PID  USER   FD  TYPE DEVICE SIZE/OFF NODE NAME

nginx  10523  root    6u IPv4  19670      0t0 TCP *:http (LISTEN)

nginx   10524nginx    6u  IPv4 19670      0t0  TCP *:http (LISTEN)

# netstat -lntup | grep 80

tcp       0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      10523/nginx

如启动nginx报错(pcre通过yum安装不会出现此问题)

/application/nginx/sbin/nginx: error while loadingshared libraries: libpcre.so.1: cannot open shared object file: No such file ordirectory

解决办法:

tail -1 /etc/ld.so.conf

/usr/local/lib/

ldconfig

创建虚拟主机

# mkdir -p /app/zpy/nginx/html/{www,blog,bbs} 

# for name in www blog bbs;do echo "hello world from $name">/app/zpy/nginx/html/$name/index.html;done

# for name in www blog bbs;do cat /app/zpy/nginx/html/$name/index.html;done

修改nginx.conf配置

# cd /app/zpy/nginx/conf/

# grep -vE "#|^$" nginx.conf.default>nginx.conf

# vim nginx.conf

worker_processes 1;

error_log logs/error.log error;

events {

   worker_connections  1024;

}

http {

   include       mime.types;

   default_type application/octet-stream;

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';



   sendfile        on;

   keepalive_timeout  65;

   include extra/*.conf;

  

    fastcgi_cache_path /app/zpy/nginx/fastcgi_cache/ levels=1:2 keys_zone=TEST:10M inactive=5m;

    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;


    server {

       listen       80;

        location/ {

           root   html;

           index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

        root   html;

        }

编写虚拟主机配置文件。

# cat /app/zpy/nginx/conf/extra/www.conf 

server {

    listen 8001;

    server_name www.nginx_1.com;

    root html/www;

    index index.html index.htm index.php;

    access_log logs/www_access.log main;

    location ~ .*\.(php|php5)?$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

}

# cat /app/zpy/nginx/conf/extra/bbs.conf 

server {

    listen 8002;

    server_name nginx_1.com;

    location / {

deny all;

}

}


server {

    listen 8002;

    server_name bbs.nginx_1.com;

    location / {

root html/bbs;

index bbs.html;

access_log logs/bbs_access.log main;

}

}

# cat /app/zpy/nginx/conf/extra/blog.conf

server {

    listen 8003;

    server_name blog.nginx_1.com;

    auth_basic "hello all";

    auth_basic_user_file /app/zpy/nginx/db/passwd.db;

    location / {

root html/blog;

index blog.html;

access_log logs/blog_access.log main;

}

}   

# ../sbin/nginx -t

nginx: the configuration file/application/nginx1.6.2/conf/nginx.conf syntax is ok

nginx: configuration file/application/nginx1.6.2/conf/nginx.conf test is successful

# ../sbin/nginx -s reload

本地检查,别忘了配置好/etc/hosts


2.Mysql 安装配置

这里我使用自己编写的安装脚本进行了安装。

# pwd

/app/3rd/mysql-install

# ls

cmake-2.8.5.tar.gz  my.cnf  mysql-5.5.39.tar.gz  mysql_install.sh

# vim mysql_install.sh

#!/bin/bash

###this is a script for install mysql 

###zxy   2015-07-09###

. /etc/init.d/functions



package_cmake=cmake-2.8.5

package_mysql=mysql-5.5.39


package_dir=/app/3rd

install_dir=/usr/local

backup_dir=/opt/backup


[ ! -d $package_dir ] && {

mkdir -p $package_dir

echo "please put the install-package in $package_dir/mysql-install/"

exit 1

}

[ ! -d $install_dir ] && mkdir $install_dir

[ ! -d $backup_dir ]  && mkdir $backup_dir


#mysql user

echo "###   now create mysql user   ###"

id mysql > /dev/null 2>&1

[ $? -ne 0 ] && {

groupadd -g 888 mysql

useradd -u 888 -g 888 -s /sbin/nologin -M mysql

action "mysql user" /bin/true

} || {

action "mysql user" /bin/false

echo "mysql user already exists"

}


#install prepare

echo "###   now install prepare   ###"

yum clean all > /dev/null 2>&1

yum list > /dev/null 2>&1

for yum_install in gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel*  ncurses-devel

do

yum install -y $yum_install > /dev/null 2>&1

done

[ $? -eq 0 ] && {

action "install prepare" /bin/true

} || {

action "install prepare" /bin/false

echo "please check up your yum packages"

exit 1

}


#install cmake

echo "###   now install cmake   ###"

cd $package_dir/mysql-install

[ ! -f $package_cmake.tar.gz ] && {

action "install cmake" /bin/false

echo "please download cmake package"

exit 1

}


tar zxvf $package_cmake.tar.gz -C $package_dir > /dev/null 2>&1

cd $package_dir/$package_cmake/

./configure --prefix=$install_dir/cmake > /dev/null 2>&1

[ $? -ne 0 ] && {

action "install cmake" /bin/false

echo "the configure is failed"

exit 1

}


gmake > /dev/null 2>&1

[ $? -ne 0 ] && {

action "install cmake" /bin/false

echo "the gmake is failed"

exit 1

}


make install > /dev/null 2>&1

[ $? -ne 0 ] && {

action "install cmake" /bin/false

echo "the make install is failed"

exit 1

}


action "install cmake" /bin/true


profile_cmake_count=`grep -E "mysql|cmake" /etc/profile | wc -l`

[ $profile_cmake_count -eq 0 ] && {

cat >> /etc/profile <<END

export PATH=$install_dir/cmake:$PATH

export PATH=$install_dir/cmake/bin:$PATH

END

. /etc/profile

} || {

. /etc/profile

}


#install mysql

echo "###   now install $package_mysql   ###"

cd $package_dir/mysql-install

[ ! -f $package_mysql.tar.gz ] && {

action "install mysql" /bin/false

echo "please download mysql package"

exit 1

}


tar zxvf $package_mysql.tar.gz -C $package_dir > /dev/null 2>&1

cd $package_dir/$package_mysql/

cmake  -DCMAKE_INSTALL_PREFIX=$install_dir/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=$install_dir/mysql/data -DMYSQL_USER=mysql > /dev/null 2>&1

[ $? -ne 0 ] && {

action "install $package_mysql" /bin/false

echo "the configure is failed" 

exit 1

}

make > /dev/null 2>&1

[ $? -ne 0 ] && {

action "install $package_mysql" /bin/false

echo "the make is failed"

exit 1

}


make install > /dev/null 2>&1

[ $? -ne 0 ] && {

action "install $package_mysql" /bin/false

echo "the make install is failed"

exit 1

}


cp $install_dir/mysql/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld


#[ -f /etc/my.cnf ] && {

#mv /etc/my.cnf $backup_dir/my.cnf.bak

#cp my.cnf /etc/

#} || {

#cp my.cnf /etc/

#}

if [ -f /etc/my.cnf ]

then

        mv /etc/my.cnf $backup_dir/my.cnf.bak

        cp $package_dir/mysql-install/my.cnf /etc/

else

        cp $package_dir/mysql-install/my.cnf /etc/

fi



profile_mysql_count=`grep  "mysql" /etc/profile | wc -l`

[ $profile_mysql_count -eq 0 ] && {

cat >> /etc/profile <<END

export PATH=$install_dir/mysql/bin:$PATH

END

. /etc/profile

} || {

. /etc/profile

}


action "install $package_mysql" /bin/true



#mysql initialize

echo "###   now mysql initialize   ###"

#[ ! -f $install_dir/mysql/log-bin ]  &&  mkdir $install_dir/mysql/log-bin 

mkdir $install_dir/mysql/log-bin > /dev/null 2>&1

mkdir $install_dir/mysql/pid > /dev/null 2>&1

chown -R mysql.mysql $install_dir/mysql

$install_dir/mysql/scripts/mysql_install_db --basedir=$install_dir/mysql --datadir=$install_dir/mysql/data --user=mysql > /dev/null 2>&1

[ $? -ne 0 ] && {

action "mysql initialize" /bin/false

echo "the mysql initialize is failed"

exit 1

} || {

action "mysql initialize" /bin/true

}


echo "###   now mysql initial landing   ###"

chkconfig --add mysqld

chkconfig mysqld on

/etc/init.d/mysqld start

echo "update user set password=password('1qaz@WSX?') where user='root';flush privileges;" | mysql -uroot mysql

[ $? -ne 0 ] && {

action "mysql initial landing" /bin/false

echo "the mysql initial landing is failed"

exit 1

} || {

action "mysql initial landing" /bin/true

}



3.PHP 安装配置

安装依赖包

# cd /app/3rd

# for i in install opensslopenssl-devel zlib libxml libjpeg freetype libpng gd  curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-develcurl-devel ;do yum install -y $i ; done

# yum install libxslt* -y

安装iconv

# tar xf libiconv-1.14.tar.gz

# cd libiconv-1.14

# ./configure--prefix=/app/3rd/libiconv

# make && makeinstall

安装libmcrypt

# cd ../

# tar xf libmcrypt-2.5.8.tar.gz

# cd libmcrypt-2.5.8

# ./configure

# make && makeinstall

# cd libltdl/

# ./configure--enable-ltdl-install

# make && make install

安装mhash

# wgethttp://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz

# tar xf mhash-0.9.9.9.tar.gz

# cd mhash-0.9.9.9

# ./configure

# make && makeinstall

安装mcrypt-2.6.8

wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz

# tar xf mcrypt-2.6.8.tar.gz

# cd mcrypt-2.6.8

# ./configureLD_LIBRARY_PATH=/usr/local/lib

# make && makeinstall

配置

rm -f /usr/lib64/libmcrypt.*
rm -f /usr/lib64/libmhash*
ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

安装php(LNMP环境编译参数

# tar xf php-5.3.27.tar.gz

# cd php-5.3.27

# ./configure --prefix=/app/zpy/php5.3.27 --with-mysql=/usr/local/mysql --with-iconv-dir=/app/3rd/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=zpy --with-fpm-group=zpy --enable-ftp


Php安装报错(对于5.3以下版本):
1)Thank you for using PHP.Notice: Following unknown configure options wereused:–enable-fpm

这是因为没有打补丁
tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
如果没有打补丁的话,那么安装目录下少sbin目录

 

 

对于报错libjpeg.(a|so) not found.

解决:

#cp -frp /usr/lib64/libpng* /usr/lib/

[root@c66-LNMP php-5.3.27]# ln -s/application/mysql/lib/libmysqlclient.so.18 /usr/lib64/

make

make isntall

ln -s /application/php5.3.27/ /application/php

cp php.ini-production /application/php/lib/php.ini   正式环境配置文件


配置php及fcgi进程管理

ln -s /app/zpy/php5.3.27/ /app/zpy/php

cp php.ini-production /app/zpy/php/lib/php.ini

拷贝事先准备好的配置文件

百度云:http://pan.baidu.com/s/1c14eYfY

# pwd

/app/3rd/19-lnmp-conf/php/etc

#vim diff php-fpm.conf /app/zpy/php/etc/php-fpm.conf.default

cp php-fpm.conf /app/zpy/php/etc/

启动服务

mkdir-p /app/logs

/app/zpy/php/sbin/php-fpm-t

[13-Jan-2015 21:17:06] NOTICE: configuration file/application/php5.3.27/etc/php-fpm.conf test is successful

#/application/php/sbin/php-fpm

# netstat -lntup | grep php-fpm

tcp       0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      5230/php-fpm       

# ps -ef | grep php-fpm

root     14263     1  0 Jul20 ?        00:00:03 php-fpm: master process (/app/zpy/php5.3.27/etc/php-fpm.conf)

zpy      14264 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14265 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14266 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14267 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14268 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14269 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14270 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14271 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14272 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14273 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14274 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14275 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14276 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14277 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14278 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

zpy      14279 14263  0 Jul20 ?        00:00:00 php-fpm: pool www        

root     24520 13309  0 10:00 pts/1    00:00:00 grep php-fpm


4.配置PHP整合nginx

配置开机自启

# cat /etc/rc.local

#nginx+php-fpm

/etc/init.d/mysqld start

/app/zpy/php/sbin/php-fpm

/app/zpy/nginx/sbin/nginx -s reload


配置nginx

见上面nginx配置处的配置文件。


测试

# cat /app/zpy/nginx/html/www/index.php

<?php

phpinfo();

?>

访问 http://www.nginx_1.com:8001


lnmp php 能不能读写mysql

# cat /app/zpy/nginx/html/bbs/index.php

<?php

$link_id=mysql_connect('localhost','root','1qaz@WSX?') or mysql_error();

if($link_id){

echo "mysql successful by root !\n";

}else{

echo "mysql_error()";

}

?>

# /app/zpy/php/bin/phpindex.php

mysql successful by root !


5.安装php的扩展模块

安装包的百度云地址:http://pan.baidu.com/s/1eRRb5qq

1.memcache //高性能的分布式的内存对象缓存系统。

# cd /app/3rd/nginx

# tar xvzf memcache-2.2.5.tgz

# cd memcache-2.2.5 

# /app/zpy/php/bin/phpize

# ./configure --with-php-config=/app/zpy/php/bin/php-config

# make

# make install

 

2.eaccelerator //一个开源的php加速器

# tar xvjf eaccelerator-0.9.6.1.tar.bz2

# cd eaccelerator-0.9.6.1

# /app/zpy/php/bin/phpize

# ./configure --enable-eaccelerator=shared--with-php-config=/app/zpy/php/bin/php-config

# make

# make install

 

3.PDO-MYSQL //连接mysql数据库

# tar xvzf PDO_MYSQL-1.0.2.tgz 

# cd PDO_MYSQL-1.0.2/

# /app/zpy/php/bin/phpize

# ./configure --with-php-config=/app/zpy/php/bin/php-config--with-pdo-mysql=/usr/local/mysql

# make

# make install

 

4.ImageMagick //画像或图片処理

# tar xvzf ImageMagick.tar.gz 

# cd ImageMagick-6.5.1-2/

# ./configure

# make

# make install

报错:[PerlMagick/Makefile] 错误 2

解决:yum -y install perl-CPAN

 

5.imagick //连接ImageMagick软件

# tar xvzf imagick-2.3.0.tgz 

# cd imagick-2.3.0/

# /app/zpy/php/bin/phpize

# ./configure --with-php-config=/app/zpy/php/bin/php-config

# make

# make install

 

配置php

修改php相关的配置文件

# cd /app/zpy/php/etc

# vim php.ini

output_buffering = On

extension_dir ="/app/zpy/php/lib/php/extensions/no-debug-non-zts-20060613/"

extension = "memcache.so"

extension = "pdo_mysql.so"

extension = "imagick.so"

cgi.fix_pathinfo=0


实现eaccelerator加速

# mkdir -p /app/zpy/php5.3.27/eaccelerator_cache

# vim /app/zpy/php/etc/php.ini

在文件的末尾添加:

[eaccelerator]

zend_extension="/app/zpy/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"

eaccelerator.shm_size="64"

eaccelerator.cache_dir="/app/zpy/php5.3.27/eaccelerator_cache"

eaccelerator.enable="1"

eaccelerator.optimizer="1"

eaccelerator.check_mtime="1"

eaccelerator.debug="0"

eaccelerator.filter=""

eaccelerator.shm_max="0"

eaccelerator.shm_ttl="3600"

eaccelerator.shm_prune_period="3600"

eaccelerator.shm_only="0"

eaccelerator.compress="1"

eaccelerator.compress_level="9"

 

3,配置php-fpm.conf,将安装包里的php-fpm.conf文件,覆盖原有文件:

# cp php-fpm.conf /usr/local/webserver/php/etc/php-fpm.conf


6.科普

安装(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize

    phpize是干嘛的?

phpize是什么东西呢?php官方的说明:
http://php.net/manual/en/install.pecl.phpize.php
phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块
比如你想在原来编译好的php中加入memcached或者ImageMagick等扩展模块,可以使用phpize,通过以下几步工作。

如何使用phpize

php编译完成后,phpbin目录下会有phpize这个脚本文件。在编译你要添加的扩展模块之前,执行以下phpize就可以了;

比如现在想在php中加入memcache扩展模块:我们要做的只是如下几步
————————————————————————
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/webserver/php/bin/phpize
./configure with-php-config=/usr/local/webserver/php/bin/php-config
make
make install

 

注意./configure 后面可以指定的是php-config文件的路径
这样编译就完成了,还需要做的是在php.ini文件中加入extension
extension = memcache.so

随着工作的需要,我们还要再加些模块时,又不想重新编译php,这是可以使用phpize了。

eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍。

php.ini文件中,加入;

zend_extension="/usr/lib/php/extensions/eaccelerator.so"注:模块的具体位置;

eaccelerator.shm_size="16" 注:共享内存大小;

eaccelerator.cache_dir="/home/eaccelerator"注:缓存目录 


PDO扩展为PHP访问数据库定义了一个轻量级的、一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数据。 


ImageMagick是一个免费的创建、编辑、合成图片的软件。它可以读取、转换、写入多种格式的图片。图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线,多边形,椭圆,曲线,附加到图片伸展旋转。ImageMagick是免费软件:全部源码开放,可以自由使用,复制,修改,发布,它遵守GPL许可协议,可以运行于大多数的操作系统,ImageMagick的大多数功能的使用都来源于命令行工具。


imagick 连接ImageMagick软件



本文转自 周新宇1991 51CTO博客,原文链接:http://blog.51cto.com/zhouxinyu1991/1828284,如需转载请自行联系原作者


网友评论

登录后评论
0/500
评论
技术小阿哥
+ 关注