LNMP环境搭建

LNMP环境

1.安装Linux操作系统(略)

2.安装Mysql

我们平时都是用源码包进行编译安装,这种方式比较灵活可控,但是比较麻烦耗费的时间很长。我们也可以通过二进制包进行安装或者直接使用rpm方式进行安装

Mysql软件下载地址:http://dev.mysql.com/downloads/

源码安装请参考

mysql安装一 — 源码包安装.md

rpm安装方式

(1)下载所需的安装包

安装包:https://yunpan.cn/cMqfzAiJHpGdy 访问密码 ba7f

(2)执行安装

  1. rpm -ivh MySQL-server-5.5.51-1.el6.x86_64.rpm --force
  2. rpm -ivh MySQL-client-5.5.51-1.el6.x86_64.rpm

(3)启动服务

  1. /etc/init.d/mysql start
  2. /etc/init.d/mysql status

(4)查看进程并修改密码

  1. netstat -nat | grep 3306
  2. /usr/bin/mysqladmin -u root password
  3. mysql -uroot -p

3.安装php

php官方下载地址: http://www.php.net/downloads.php

(1)创建用户

  1. useradd -s /sbin/nologin php-fpm

(2)安装所需的依赖包

  1. yum install gcc -y
  2. yum install libxslt-devel
  3. yum install libjpeg-devel
  4. yum install libpng-devel
  5. ps -ef | grep mysql
  6. yum install curl-devel
  7. yum install libXpm-devel
  8. yum install libfreetype6-dev
  9. yum install freetype-deve
  10. 安装mcrypt
  11. tar -xzvf libmcrypt-2.5.7.tar.gz
  12. ./configure --prefix=/usr/local/libmcrypt
  13. make && make install

(3)解压

  1. tar -xzvf php-5.5.38.tar.gz
  2. cd php-5.5.38

(4)配置编译程序

  1. ./configure \
  2. --prefix=/usr/local/php \
  3. --with-config-file-path=/usr/local/php/etc \
  4. --enable-fpm \
  5. --with-fpm-user=php-fpm \
  6. --with-fpm-group=php-fpm \
  7. --with-mysql \
  8. --with-mysql-sock \
  9. --with-libxml-dir \
  10. --with-gd \
  11. --with-jpeg-dir \
  12. --with-png-dir \
  13. --with-freetype-dir \
  14. --with-iconv-dir \
  15. --with-zlib-dir \
  16. --with-mcrypt=/usr/local/libmcrypt \
  17. --enable-soap \
  18. --enable-gd-native-ttf \
  19. --enable-ftp \
  20. --enable-mbstring \
  21. --enable-exif \
  22. --enable-zend-multibyte \
  23. --disable-ipv6 \
  24. --with-pear \
  25. --with-curl \
  26. --with-openssl \
  27. --enable-bcmath \
  28. --enable-sockets \
  29. --with-gettext

(5)编译并安装php

  1. make && make install
  2. make test

(6)修改配置文件

  1. cp php.ini-production /usr/local/php/etc/php.ini
  2. vi /usr/local/php/etc/php-fpm.conf
  3. 把下面内容加入
  4. [global]
  5. pid = /usr/local/php/var/run/php-fpm.pid
  6. error_log = /usr/local/php/var/log/php-fpm.log
  7. [www]
  8. #listen = /tmp/php-fcgi.sock
  9. listen = 127.0.0.1:9000
  10. user = php-fpm
  11. group = php-fpm
  12. pm = dynamic
  13. pm.max_children = 50
  14. pm.start_servers = 20
  15. pm.min_spare_servers = 5
  16. pm.max_spare_servers = 35
  17. pm.max_requests = 500
  18. rlimit_files = 1024
  19. request_terminate_timeout = 300
  20. 保存修改后,通过下面命令检查配置是否正确
  21. /usr/local/php/sbin/php-fpm -t

(7)启动php

  1. cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  2. chmod 755 /etc/init.d/php-fpm
  3. service php-fpm start
  4. 开机启动
  5. chkconfig php-fpm on
  6. ps aux |grep php-fpm

参考文章:

  1. 第18章 LNMP环境搭建 –Linux 学习的好网站
  1. LNMP环境搭建-php

  2. 编译php的常见错误:http://lyp.cn/350_how-to-fix-php-compile-errors

4.编译时报错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决:
–with-mcrypt参数加上路径–with-mcrypt=/usr/local/libmcrypt

4.安装Nginx


可以到网盘目录进行下载:
https://yunpan.cn/cMqmvitt4Ezqj 访问密码 58ff

(1)下载

Nginx官方网站(http://nginx.org)

(2)解压nginx

  1. tar zxvf nginx-1.4.4.tar.gz

(3)配置编译参数

  1. cd nginx-1.4.4
  2. ./configure \
  3. --prefix=/usr/local/nginx \
  4. --with-http_realip_module \
  5. --with-http_sub_module \
  6. --with-http_gzip_static_module \
  7. --with-http_stub_status_module \
  8. --with-pcre

(4)编译nginx

  1. make

(5)安装nginx

  1. make install

因为nginx比较小,所以很快就会安装完,而且也不会出什么错误,如果出错了,自行百度Google

(6)编写nginx启动脚本,并加入系统服务

  1. vim /etc/init.d/nginx

写入如下内容:

  1. #!/bin/bash
  2. # chkconfig: - 30 21
  3. # description: http service.
  4. # Source Function Library
  5. . /etc/init.d/functions
  6. # Nginx Settings
  7. NGINX_SBIN="/usr/local/nginx/sbin/nginx"
  8. NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
  9. NGINX_PID="/usr/local/nginx/logs/nginx.pid"
  10. RETVAL=0
  11. prog="Nginx"
  12. start() {
  13. echo -n $"Starting $prog: "
  14. mkdir -p /dev/shm/nginx_temp
  15. daemon $NGINX_SBIN -c $NGINX_CONF
  16. RETVAL=$?
  17. echo
  18. return $RETVAL
  19. }
  20. stop() {
  21. echo -n $"Stopping $prog: "
  22. killproc -p $NGINX_PID $NGINX_SBIN -TERM
  23. rm -rf /dev/shm/nginx_temp
  24. RETVAL=$?
  25. echo
  26. return $RETVAL
  27. }
  28. reload(){
  29. echo -n $"Reloading $prog: "
  30. killproc -p $NGINX_PID $NGINX_SBIN -HUP
  31. RETVAL=$?
  32. echo
  33. return $RETVAL
  34. }
  35. restart(){
  36. stop
  37. start
  38. }
  39. configtest(){
  40. $NGINX_SBIN -c $NGINX_CONF -t
  41. return 0
  42. }
  43. case "$1" in
  44. start)
  45. start
  46. ;;
  47. stop)
  48. stop
  49. ;;
  50. reload)
  51. reload
  52. ;;
  53. restart)
  54. restart
  55. ;;
  56. configtest)
  57. configtest
  58. ;;
  59. *)
  60. echo $"Usage: $0 {start|stop|reload|restart|configtest}"
  61. RETVAL=1
  62. esac
  63. exit $RETVAL

保存后,更改权限:

  1. chmod 755 /etc/init.d/nginx
  2. chkconfig --add nginx

如果想开机启动,请执行:

  1. chkconfig nginx on

(7)更改nginx配置

首先把原来的配置文件清空:

  1. > /usr/local/nginx/conf/nginx.conf

“>” 这个符号为重定向的意思,单独用它,可以把一个文本文档快速清空。

  1. vi /usr/local/nginx/conf/nginx.conf

写入如下内容:

  1. user nobody nobody;
  2. worker_processes 2;
  3. error_log /usr/local/nginx/logs/nginx_error.log crit;
  4. pid /usr/local/nginx/logs/nginx.pid;
  5. worker_rlimit_nofile 51200;
  6. events
  7. {
  8. use epoll;
  9. worker_connections 6000;
  10. }
  11. http
  12. {
  13. include mime.types;
  14. default_type application/octet-stream;
  15. server_names_hash_bucket_size 3526;
  16. server_names_hash_max_size 4096;
  17. log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  18. '$host "$request_uri" $status'
  19. '"$http_referer" "$http_user_agent"';
  20. sendfile on;
  21. tcp_nopush on;
  22. keepalive_timeout 30;
  23. client_header_timeout 3m;
  24. client_body_timeout 3m;
  25. send_timeout 3m;
  26. connection_pool_size 256;
  27. client_header_buffer_size 1k;
  28. large_client_header_buffers 8 4k;
  29. request_pool_size 4k;
  30. output_buffers 4 32k;
  31. postpone_output 1460;
  32. client_max_body_size 10m;
  33. client_body_buffer_size 256k;
  34. client_body_temp_path /usr/local/nginx/client_body_temp;
  35. proxy_temp_path /usr/local/nginx/proxy_temp;
  36. fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  37. fastcgi_intercept_errors on;
  38. tcp_nodelay on;
  39. gzip on;
  40. gzip_min_length 1k;
  41. gzip_buffers 4 8k;
  42. gzip_comp_level 5;
  43. gzip_http_version 1.1;
  44. gzip_types text/plain application/x-javascript text/css text/htm application/xml;
  45. server
  46. {
  47. listen 80;
  48. server_name localhost;
  49. index index.html index.htm index.php;
  50. root /usr/local/nginx/html;
  51. location ~ \.php$ {
  52. include fastcgi_params;
  53. fastcgi_pass unix:/tmp/php-fcgi.sock;
  54. -----fastcgi_pass 127.0.0.1:9000;
  55. -----#listen = /tmp/php-fcgi.sock
  56. fastcgi_index index.php;
  57. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
  58. }
  59. }
  60. }

如果启动报/tmp/php-fcgi.sock不存在,把参数文件中的fastcgi_pass unix: /tmp/php-fcgi.sock; 修改成fastcgi_pass unix: 127.0.0.1:9000; 这个参数和php-fpm.com中的listen = /tmp/php-fcgi.sock是对应的

保存配置后,先检验一下配置文件是否有错误存在:

  1. /usr/local/nginx/sbin/nginx -t

如果显示内容如下,则配置正确,否则需要根据错误提示修改配置文件:

  1. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  2. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动nginx:

  1. service nginx start

如果不能启动,请查看“/usr/local/nginx/logs/error.log”文件,检查nginx是否启动:

  1. ps aux |grep nginx

看是否有进程。

测试nginx是否能解析php文件

php文件放在 /usr/local/nginx/conf/nginx.conf 配置文件root /usr/local/nginx/html;所指的目录下

如:

  1. vi /usr/local/nginx/html/2.php
  2. <?php
  3. echo "hellp php"
  4. ?>

或者

  1. vi /usr/local/nginx/html/2.php
  2. <?php
  3. phpinfo();
  4. ?>
  1. [root@localhost html]# curl localhost/2.php -I
  2. HTTP/1.1 200 OK
  3. Server: nginx/1.4.7
  4. Date: Thu, 25 Aug 2016 01:10:51 GMT
  5. Content-Type: text/html
  6. Connection: keep-alive

输出200 OK即正常解析!

在浏览器中测试

参考:

30分钟搭建LNMP开发环境

第18章 LNMP环境搭建





posted @ 2017-03-19 11:52  hao_xiaoyu  阅读(587)  评论(0编辑  收藏  举报