Linux环境从头搭建LNMP,步骤很详细

环境版本为

  • nginx 1.15.8

  • php 7.2.0

  • mysql  5.7.22

一、先安装Nginx

1、安装依赖包

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2、下载并解压安装包

wget http://nginx.org/download/nginx-1.15.8.tar.gz #下载
tar -xvf nginx-1.15.8.tar.gz #解压

3.安装nginx

cd nginx-1.15.8
./configure --prefix=/usr/local/nginx  --with-http_stub_status_module  --with-http_gzip_static_module --with-http_realip_module --with-http_su
b_module  --with-http_ssl_module --with-http_realip_module  --with-http_sub_module  --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module  --with-http_slice_module
make
make install

4、启动Nginx

/usr/local/nginx/sbin/nginx

5、访问域名

Linux环境从头搭建LNMP,步骤很详细_第1张图片

二、安装PHP

1、安装依赖

yum install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libzip-devel pcre-devel libxslt-devel*

2、下载并解压安装包

wget http://php.net/get/php-7.2.0.tar.gz/from/a/mirror
tar -zxvf php-7.2.0.tar.gz

3、安装PHP

./configure --prefix=/usr/local/php7.2.0 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
make
make install

4、把PHP加入环境变量

cp /usr/local/php7.2.0/bin/php /usr/local/bin/

5、配置php-fpm启动文件

  • 复制一份php-fpm.conf 文件到etc下面
  • 更改一下默认配置文件的名称

     

cp /root/php-7.2.0/sapi/fpm/php-fpm.conf /usr/local/php7.2.0/etc/php-fpm.conf
mv /usr/local/php7.2.0/etc/php-fpm.d/www.conf.default /usr/local/php7.2.0/etc/php-fpm.d/www.conf
vim /etc/init.d/php-fpm
#!/bin/bash

if [[ $1 = 'stop' ]];
then
  kill -INT `cat /usr/local/php7.2.0/var/run/php-fpm.pid`
  echo -e "Stopping php-fpm is success"
elif [[ $1 = 'start' ]];
then
  /usr/local/php7.2.0/sbin/php-fpm
  echo -e "Starting php-fpm is success"
elif [[ $1 = 'restart' ]];
then
  kill -USR2 `cat /usr/local/php7.2.0/var/run/php-fpm.pid`
  echo -e "Restart php-fpm is success"
else
  echo -e "Usage: /etc/init.d/php-fpm {start|stop|restart}"
fi

chmod +x php-fpm #给php-fpm 可执行的权限

三、nginx配置访问php

1、修改nginx的配置文件

worker_processes        8;
error_log               /usr/local/nginx-1.15.8/logs/error.log;
pid                     logs/nginx.pid;

worker_rlimit_nofile    60000;
events {
use                     epoll;
worker_connections      10000;
}

http {
      include           mime.types;
      default_type      application/octet-stream;
      sendfile          on;
      tcp_nopush        on;
      server_tokens     off;
      send_timeout      30;
      fastcgi_connect_timeout 300;
      fastcgi_send_timeout    300;
      fastcgi_read_timeout    300;
      keepalive_timeout       30;
      fastcgi_buffers 8 128k;
      reset_timedout_connection on;
      client_header_buffer_size      32k;
      large_client_header_buffers 4 128k;
      client_max_body_size 60m;
      add_header Backend srv-2;
      gzip  on;
      gzip_min_length  1k;
      gzip_buffers     4 8k;
      gzip_http_version 1.1;
      gzip_types	text/plain text/css application/json text/xml application/xml application/javascript text/javascript application/x-javascript image/png image/jpeg;

      log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
	'$status ${request_time}s $body_bytes_sent $request_body '
	' "$http_referer" "$http_user_agent" $http_x_forwarded_for';

      include vhosts/*.conf;
      fastcgi_intercept_errors on;
}

2、创建vhosts文件夹(以后所有的网站配置都放在这下面)

这是模板文件

server {
	listen        80;
	server_name  baidu.com;
	root   "D:/www/hjgmtool.kt007.com/backend/web";
	location / {
		index index.php index.html;
		error_page 400 /error/400.html;
		error_page 403 /error/403.html;
		error_page 404 /error/404.html;
		error_page 500 /error/500.html;
		error_page 501 /error/501.html;
		error_page 502 /error/502.html;
		error_page 503 /error/503.html;
		error_page 504 /error/504.html;
		error_page 505 /error/505.html;
		error_page 506 /error/506.html;
		error_page 507 /error/507.html;
		error_page 509 /error/509.html;
		error_page 510 /error/510.html;
		autoindex  off;
	}
	location ~ \.php(.*)$ {
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		fastcgi_param  PATH_INFO  $fastcgi_path_info;
		fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
		include        fastcgi_params;
	}
	if (-f $request_filename/index.html){
	  rewrite (.*) $1/index.html break;
	}
	if (-f $request_filename/index.php){
		rewrite (.*) $1/index.php;
	}
	if (!-f $request_filename){
		rewrite (.*) /index.php last;
	}
}

四、安装MySQL

见链接:

https://blog.csdn.net/qq_21720233/article/details/103837936

五、测试

connect('localhost','root','123456','testdb');
$mysqli->set_charset("utf8");
$sql="select * from user";
$rs=$mysqli->query($sql);
while($row=mysqli_fetch_array($rs,MYSQLI_ASSOC)){
    $c=$row['username'];
    echo "$c";
}

 

你可能感兴趣的:(技术工具)