编译安装LNMP(linux+nginx+mysql+php)环境详解

爱互踩 爱互踩流量交换~

LNMP环境安装方式不少种了,这里给各位介绍编译安装LNMP(linux+nginx+mysql+php)配置,有兴趣使用编译安装LNMP(linux+nginx+mysql+php)的朋友可以和小编来看看吧.

说明:php在编译安装时,nginx要想能够调用php提供动态php格式的网页,必须用FastCGI来实现,但 FastCGI只是一个框架,实现FastCGI框架的有PHP-FPM,但对于5.2.x版本的php来说,默认是不支持PHP-FPM的,需要打上php-fpm的补丁,对于5.3.2之前版本的也是需要打补丁的,而且打补丁的过程比较麻烦。好在5.3.3版 本的PHP-FPM被直接做进了源代码包中,在编译安装时只需启用PHP-FPM功能即可。

但如果要使用PHP-FPM的话,还需要提供以下几个功能:

需要提供可以解析xml格式的文档,需要安装libxml2 和libxml2-devel这两个包,好在这两个包在安装完开发环境后这两个包是默认安装过的.

需要安装libevent并且在1.4.12之后的版本,不幸的是rhel5.4版本中这个包是是在1.4.12之前的,需要从新手动编译安装该包.

libiconv 用来提供网络连接方式的功能组件,可以实现更快速的网络访问,这个组件系统上是没有装的,需要手动编译安装.

构建编译环境:

  1. yum-yinstallgccopenssl-develzlib-develpcre-devel
  2. yumgroupinstall"DevelopementTools""DevelopmentLibraries"-yt

首先安装Nginx:

  1. wgethttp://nginx.org/download/nginx-1.0.14.tar.gz
  2. tarzxvfnginx-1.0.14.tar.gz#
  3. useradd-s/sbin/nologin-Mnginx
  4. cdnginx-1.0.14
  5. ./configure\
  6. --prefix=/usr\
  7. --sbin-path=/usr/sbin/nginx\
  8. --conf-path=/etc/nginx/nginx.conf\
  9. --error-log-path=/var/log/nginx/error.log\
  10. --http-log-path=/var/log/nginx/access.log\
  11. --pid-path=/var/run/nginx/nginx.pid\
  12. --lock-path=/var/lock/nginx.lock\
  13. --user=nginx\
  14. --group=nginx\
  15. --with-http_ssl_module\
  16. --with-http_flv_module\
  17. --with-http_stub_status_module\
  18. --with-http_gzip_static_module\
  19. --http-client-body-temp-path=/var/tmp/nginx/client/\
  20. --http-proxy-temp-path=/var/tmp/nginx/proxy/\
  21. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
  22. make&&makeinstall

创建nginx的启动脚本:

  1. vim/etc/init.d/nginxd
  2. #!/bin/sh
  3. #
  4. #nginx-thisscriptstartsandstopsthenginxdaemon
  5. #
  6. #chkconfig:-8515
  7. #description:NginxisanHTTP(S)server,HTTP(S)reverse\
  8. #proxyandIMAP/POP3proxyserver
  9. #processname:nginx
  10. #config:/etc/nginx/nginx.conf
  11. #config:/etc/sysconfig/nginx
  12. #pidfile:/var/run/nginx.pid
  13. #Sourcefunctionlibrary.
  14. ./etc/rc.d/init.d/functions
  15. #Sourcenetworkingconfiguration.
  16. ./etc/sysconfig/network
  17. #Checkthatnetworkingisup.
  18. ["$NETWORKING"="no"]&&exit0
  19. nginx="/usr/sbin/nginx"
  20. prog=$(basename$nginx)
  21. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  22. [-f/etc/sysconfig/nginx]&&./etc/sysconfig/nginx
  23. lockfile=/var/lock/subsys/nginx
  24. make_dirs(){
  25. #makerequireddirectories
  26. user=`nginx-V2>&1|grep"configurearguments:"|sed's/[^*]*--user=\([^]*\).*/\1/g'-`
  27. options=`$nginx-V2>&1|grep'configurearguments:'`
  28. foroptin$options;do
  29. if[`echo$opt|grep'.*-temp-path'`];then
  30. value=`echo$opt|cut-d"="-f2`
  31. if[!-d"$value"];then
  32. #echo"creating"$value
  33. mkdir-p$value&&chown-R$user$value
  34. fi
  35. fi
  36. done
  37. }
  38. start(){
  39. [-x$nginx]||exit5
  40. [-f$NGINX_CONF_FILE]||exit6
  41. make_dirs
  42. echo-n$"Starting$prog:"
  43. daemon$nginx-c$NGINX_CONF_FILE
  44. retval=$?
  45. echo
  46. [$retval-eq0]&&touch$lockfile
  47. return$retval
  48. }
  49. stop(){
  50. echo-n$"Stopping$prog:"
  51. killproc$prog-QUIT
  52. retval=$?
  53. echo
  54. [$retval-eq0]&&rm-f$lockfile
  55. return$retval
  56. }
  57. restart(){
  58. configtest||return$?
  59. stop
  60. sleep1
  61. start
  62. }
  63. reload(){
  64. configtest||return$?
  65. echo-n$"Reloading$prog:"
  66. killproc$nginx-HUP
  67. RETVAL=$?
  68. echo
  69. }
  70. force_reload(){
  71. restart
  72. }
  73. configtest(){
  74. $nginx-t-c$NGINX_CONF_FILE
  75. }
  76. rh_status(){
  77. status$prog
  78. }
  79. rh_status_q(){
  80. rh_status>/dev/null2>&1
  81. }
  82. case"$1"in
  83. start)
  84. rh_status_q&&exit0
  85. $1
  86. ;;
  87. stop)
  88. rh_status_q||exit0
  89. $1
  90. ;;
  91. restart|configtest)
  92. $1
  93. ;;
  94. reload)
  95. rh_status_q||exit7
  96. $1
  97. ;;
  98. force-reload)
  99. force_reload
  100. ;;
  101. status)
  102. rh_status
  103. ;;
  104. condrestart|try-restart)
  105. rh_status_q||exit0
  106. ;;
  107. *)
  108. echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"//phpfensi.com
  109. exit2
  110. esac
  111. chmod+x/etc/init.d/nginxd#给予脚本执行权限
  112. chkconfig--addnginxd#加入开机启动选项中
  113. chkconfignginxdon#设置开机自动启动
  114. servicenginxdstart#启动nginx服务

测试访问:nginx

安装MySQL:

下载:

  1. wgethttp://mysql.mirrors.hoobly.com/Downloads/MySQL-5.5/mysql-5.5.22.tar.gz
  2. tarzxvfmysql-5.5.22.tar.gz
  3. cdmysql-5.5.22-linux2.6-i686
  4. /usr/sbin/groupaddmysql#添加mysql用户
  5. /usr/sbin/useradd-gmysqlmysql#添加mysql组

编译:

  1. ./configure--prefix=/usr/local/mysql/--enable-assembler--with-extra-charsets=complex--enable-thread-safe-client--with-big-tables--with-readline--with-ssl--with-embedded-server--enable-local-infile--with-plugins=partition,innobase,myisammrg
  2. make&&makeinstall

编译时出现错误:

  1. ../depcomp:line571:exec:g++:notfound
  2. make[1]:***[my_new.o]Error127
  3. make[1]:Leavingdirectory`/root/lnmpsrc/mysql-5.1.62/mysys'
  4. make:***[all-recursive]Error1

在其他安装g++的服务器上查看g++属于哪个包:

  1. [root@vps~]#find/-nameg++
  2. /usr/bin/g++
  3. [root@vps~]#rpm-qf/usr/bin/g++
  4. gcc-c++-4.4.6-3.el6.i686

可以看出g++属于gcc-c++包,安装gcc-c++:[root@vps ~]#yum install gcc-c++ -y

重新编译:

  1. ./configure--prefix=/usr/local/mysql/--enable-assembler--with-extra-charsets=complex--enable-thread-safe-client--with-big-tables--with-readline--with-ssl--with-embedded-server--enable-local-infile--with-plugins=partition,innobase,myisammrg
  2. make&&makeinstall

改变/usr/local/mysql目录用户和属组:

chown -R mysql.mysql /usr/local/mysql

创建mysql数据库、日志存放目录:

  1. mkdir/mysql/{data,binlog,relaylog}-p
  2. chown-Rmysql.mysql/mysql

以mysql帐号建立数据表:

  1. /usr/local/mysql/bin/mysql_install_db--basedir=/usr/local/mysql--datadir=/mysql/data--user=mysql

创建mysql配置文件:

  1. vim/mysql/my.cnf
  2. [client]
  3. character-set-server=utf8
  4. port=3306
  5. socket=/tmp/mysql.sock
  6. [mysqld]
  7. character-set-server=utf8
  8. replicate-ignore-db=mysql
  9. replicate-ignore-db=test
  10. replicate-ignore-db=information_schema
  11. user=mysql
  12. port=3306
  13. socket=/tmp/mysql.sock
  14. basedir=/usr/local/mysql
  15. datadir=/mysql/data
  16. log-error=/mysql/mysql_error.log
  17. pid-file=/mysql/mysql.pid
  18. open_files_limit=10240
  19. back_log=600
  20. max_connections=5000
  21. max_connect_errors=6000
  22. table_cache=614
  23. external-locking=FALSE
  24. max_allowed_packet=32M
  25. sort_buffer_size=1M
  26. join_buffer_size=1M
  27. thread_cache_size=300
  28. #thread_concurrency=8
  29. query_cache_size=512M
  30. query_cache_limit=2M
  31. query_cache_min_res_unit=2k
  32. default-storage-engine=MyISAM
  33. thread_stack=192K
  34. transaction_isolation=READ-COMMITTED
  35. tmp_table_size=246M
  36. max_heap_table_size=246M
  37. long_query_time=3
  38. log-slave-updates
  39. log-bin=/mysql/data/binlog
  40. binlog_cache_size=4M
  41. binlog_format=MIXED
  42. max_binlog_cache_size=8M
  43. max_binlog_size=1G
  44. relay-log-index=/mysql/relaylog/relaylog
  45. relay-log-info-file=/mysql/relaylog/relaylog
  46. relay-log=/mysql/relaylog/relaylog
  47. expire_logs_days=30
  48. key_buffer_size=256M
  49. read_buffer_size=1M
  50. read_rnd_buffer_size=16M
  51. bulk_insert_buffer_size=64M
  52. myisam_sort_buffer_size=128M
  53. myisam_max_sort_file_size=10G
  54. myisam_repair_threads=1
  55. myisam_recover
  56. interactive_timeout=120
  57. wait_timeout=120
  58. skip-name-resolve
  59. #master-connect-retry=10
  60. slave-skip-errors=1032,1062,126,1114,1146,1048,1396
  61. #master-host=192.168.1.1
  62. #master-user=username
  63. #master-password=password
  64. #master-port=3306
  65. server-id=1
  66. innodb_additional_mem_pool_size=16M
  67. innodb_buffer_pool_size=512M
  68. innodb_data_file_path=ibdata1:256M:autoextend
  69. innodb_file_io_threads=4
  70. innodb_thread_concurrency=8
  71. innodb_flush_log_at_trx_commit=2
  72. innodb_log_buffer_size=16M
  73. innodb_log_file_size=128M
  74. innodb_log_files_in_group=3
  75. innodb_max_dirty_pages_pct=90
  76. innodb_lock_wait_timeout=120
  77. innodb_file_per_table=0
  78. #log-slow-queries=/mysql/slow.log
  79. #long_query_time=10
  80. [mysqldump]
  81. quick
  82. max_allowed_packet=32M

管理mysql脚本:

  1. vim/mysql/mysqld
  2. #!/bin/sh
  3. mysql_port=3306
  4. mysql_username="admin"#帐号密码可以自行创建
  5. mysql_password="rootisnosafe"
  6. function_start_mysql()
  7. {
  8. printf"StartingMySQL...\n"
  9. /bin/sh/usr/local/mysql/bin/mysqld_safe--defaults-file=/mysql/my.cnf2>&1>/dev/null&
  10. }
  11. function_stop_mysql()
  12. {
  13. printf"StopingMySQL...\n"
  14. /usr/local/mysql/bin/mysqladmin-u${mysql_username}-p${mysql_password}-S/tmp/mysql.sockshutdown
  15. }
  16. function_restart_mysql()
  17. {
  18. printf"RestartingMySQL...\n"
  19. function_stop_mysql
  20. sleep5
  21. function_start_mysql
  22. }
  23. function_kill_mysql()
  24. {
  25. kill-9$(ps-ef|grep'bin/mysqld_safe'|grep${mysql_port}|awk'{printf$2}')
  26. kill-9$(ps-ef|grep'libexec/mysqld'|grep${mysql_port}|awk'{printf$2}')
  27. }
  28. if["$1"="start"];then
  29. function_start_mysql
  30. elif["$1"="stop"];then
  31. function_stop_mysql
  32. elif["$1"="restart"];then
  33. function_restart_mysql
  34. elif["$1"="kill"];then
  35. function_kill_mysql
  36. else
  37. printf"Usage:/mysql/mysqld{start|stop|restart|kill}\n"
  38. fi

赋予脚本执行权限:

chmod +x /mysql/mysqld

启动mysql:/mysql/mysqld start

命令行管理mysql:/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock

创建一个具有root权限的用户:admin,密码为rootisnosafe:

  1. GRANTALLPRIVILEGESON*.*TO'admin'@'localhost'IDENTIFIEDBY'rootisnosafe';
  2. GRANTALLPRIVILEGESON*.*TO'admin'@'127.0.0.1'IDENTIFIEDBY'rootisnosafe';

安装php:先安装libevent和libiconv:

  1. wgethttps://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz
  2. tarzxvflibevent-1.4.14b-stable.tar.gz
  3. cdlibevent-1.4.14b-stable
  4. ./configure&&make&&makeinstall
  5. wgethttp://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
  6. tarzxvflibiconv-1.14.tar.gz
  7. cdlibiconv-1.14
  8. ./configure
  9. make
  10. makeinstall
  11. ln-sf/usr/local/lib/libiconv.so.2/usr/lib/libiconv.so.2

现在安装php:

  1. wgethttp://cn.php.net/distributions/php-5.4.0.tar.gz
  2. tarzxvfphp-5.4.0.tar.gz
  3. cdphp-5.4.0
  4. ./configure--prefix=/usr/local/php--with-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config--with-openssl--enable-fpm--enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml--with-iconv-dir=/usr/local
  5. makeZEND_EXTRA_LIBS='-liconv'#因为-liconv的目录不是在/usr/local下所以安装时需要手动指定
  6. makeinstall

其中:

–with-mysql和–with-mysqli的路径是你mysql的具体所在的目录.

–enable-fpm 启动fpm,其他都是些基本选项,简单易懂.

cp php.ini-production /usr/local/php/etc/php.ini

修改配置文件:

  1. vim/usr/local/php/etc/php-fpm.conf
  2. pm.max_children=50
  3. pm.start_servers=10
  4. pm.min_spare_servers=5
  5. pm.max_spare_servers=35

启动:/usr/local/php/sbin/php-fpm &

检查是否正常启动:

  1. netstat-tunlp|grep9000
  2. tcp00127.0.0.1:90000.0.0.0:*LISTEN7826/php-fpm

将 /usr/local/php/sbin/php-fpm &加入到rc.local:

echo '/usr/local/php/sbin/php-fpm &' >>/etc/rc.local

配置fastcgi_params 文件:

vim /etc/nginx/fastcgi_params

将内容替换为:

  1. fastcgi_paramGATEWAY_INTERFACECGI/1.1;
  2. fastcgi_paramSERVER_SOFTWAREnginx;
  3. fastcgi_paramQUERY_STRING$query_string;
  4. fastcgi_paramREQUEST_METHOD$request_method;
  5. fastcgi_paramCONTENT_TYPE$content_type;
  6. fastcgi_paramCONTENT_LENGTH$content_length;
  7. fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
  8. fastcgi_paramSCRIPT_NAME$fastcgi_script_name;
  9. fastcgi_paramREQUEST_URI$request_uri;
  10. fastcgi_paramDOCUMENT_URI$document_uri;
  11. fastcgi_paramDOCUMENT_ROOT$document_root;
  12. fastcgi_paramSERVER_PROTOCOL$server_protocol;
  13. fastcgi_paramREMOTE_ADDR$remote_addr;
  14. fastcgi_paramREMOTE_PORT$remote_port;
  15. fastcgi_paramSERVER_ADDR$server_addr;
  16. fastcgi_paramSERVER_PORT$server_port;
  17. fastcgi_paramSERVER_NAME$server_name;
  18. #PHPonly,requiredifPHPwasbuiltwith--enable-force-cgi-redirect
  19. fastcgi_paramREDIRECT_STATUS200;

最后修改nginx.conf配置文件:

  1. vim/etc/nginx/nginx.conf
  2. location~\.php${
  3. root/www;
  4. fastcgi_pass127.0.0.1:9000;
  5. fastcgi_indexindex.php;
  6. fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
  7. includefastcgi_params;
  8. }
  9. location/{
  10. root/www;
  11. indexindex.phpindex.htmlindex.htm;
  12. }

保存退出,启动nginx:service nginxd start

编辑/www/index.php:

  1. <?php
  2. phpinfo();
  3. ?>
  4. //访问测试:testphp

测试数据库连接:编辑:/www/index.php

  1. <?php
  2. $link=mysql_connect("localhost","admin","rootisnosafe");
  3. if($link)echo"OK";
  4. elseecho"FAIL";
  5. ?>

刷新访问,如果出现OK字样,表示连接正常.

相关广告
  • 编译安装LNMP(linux+nginx+mysql+php)环境详解 编译安装LNMP(linux+nginx+mysql+php)环境详解 编译安装LNMP(linux+nginx+mysql+php)环境详解
相关阅读

编译安装LNMP(linux+nginx+mysql+php)环境详解

2019/10/10 17:45:13 | 谷歌SEO算法 | 搜索引擎