LNMP(Linux+nginx+MySQL+php)环境配置

1.MySQL的安装(源码包解析安装)

进行安装的目录

cd /usr/local/src/
ls

 安装一些必需的工具(yum源安装)

yum install -y vim net-tools tree

解压MySQL

tar -zxvf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz

将解压后的MySQL更改到其他目录

mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql

创建MySQL用户(不可以登录),仅是启动需要

useradd -s /sbin/nologin mysql

 进入MySQL目录

cd /usr/local/mysql/

创建一个数据存放的目录,并设置权限

mkdir -p /data/mysql #数据存放目录
chown -R mysql:mysql #设置权限

安装MySQL环境依赖

yum install -y perl-Module-Install

编译MySQL的源码

./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/

复制到配置文件my.cnf

cp support-files/my-default.cnf /etc/my.cnf
#是否覆盖 y
vim /etc/my.cnf
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 29
socket = /tmp/mysql.sock

配置启动文件

cp support-files/mysql.server /etc/init.d/mysqld
chmod 777 /etc/init.d/mysqld  #设置权限

vim /etc/init.d/mysqld
basedir=/usr/local/mysql #被注释掉了,去掉注释符号#进行修改
datadir=/data/mysql #被注释掉了,去掉注释符号#进行修改
#最后保存wq!

 将MySQL的启动文件的服务添加到chkconfig(管理系统服务中)

chkconfig --add mysqld #使得开机自启动
chkconfig mysql on #开启启动脚本服务

开启MySQL服务

service mysqld start

 查看进程服务

netstat -ntlp

 2.PHP安装与配置(源码包)

cd /usr/local/src/
ls

 安装依赖

yum install -y gcc libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel eplel-release libmcrypt-devel libcurl-devel  libjpeg-devel
#如果yum源找不到某些包,尝试切换yum源或者复制包名网上搜索yum安装代码

解压源码包

tar -zxf php-5.6.30.tar.gz

 

cd php-5.6.30

进行编译源码包

#注意查看反馈信息中是否有php-fpm的路径出现,没有很容易导致php-fpm无法进行编译,导致没有fpm无法操作。(重新进行编译或者删除源码包重新上传)

./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl

 

make && make install 

执行完make && make install会出现一个make *** 没有指明目标并且找不到 makefile。 停止的报错(Linux 命令详解(三)./configure、make、make install 命令 - Tinywan - 博客园 (cnblogs.com))有详细介绍,

解决方法

./configure #先输入这个命令
#在进行 make && make install 

 最后查看是否编译出现错误

echo $?
0

 进行配置php

复制并修改配置文件

cp php.ini-production /usr/local/php-fpm/etc/php.ini #复制配置文件
vim /usr/local/php-fpm/etc/php-fpm.conf #修改配置文件
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

创建php-fpm用户

useradd -s /sbin/nologin php-fpm
/usr/local/php-fpm/sbin/php-fpm -t

 做启动准备

cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm #给予权限
service php-fpm start #登录
chkconfig php-fpm on #启动chkconfig服务(开机启动)
ps aux |grep php-fpm #查看进程

 3.安装配置nginx(源码包)

cd /usr/local/src/
ls

 解压nginx源码压缩包

tar zxf nginx-1.17.8.tar.gz
cd nginx-1.17.8
./configure --prefix=/usr/local/nginx #对nginx源码包进行编译
make && make install
echo $?

配置nginx文件

vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() 
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL

 

chmod 755 /etc/init.d/nginx #给予权限
chkconfig --add nginx
chkconfig nginx on      #开机自启
> /usr/local/nginx/conf/nginx.conf

配置nginx的主配置文件

vim /usr/local/nginx/conf/nginx.conf
user nobody nobody;
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}

启动服务

service nginx start 

 查看进程

ps aux |grep nginx

 测试nginx

curl localhost

 

 

最后测试nginx是否可以解析php

vim /usr/local/nginx/html/1.php
<?php
        echo "test php scripts"
?>

 

 测试成功