首页 > 云开发 > PHP > 正文

后台-系统设置-扩展变量-手机广告位-内容正文顶部

lnmp环境搭建配置

PHP

一、Mysql编译安装

1.检查系统是否安装Mysql

[root@localhost /]# find -name mysql // 如果没有查找到目录信息,表示没有安装

修改iptables:vim /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 137 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT

重启iptables
service iptables restart

编译环境开始前,先安装环境编译依赖包:

yum install ncurses-devel
# 安装cmake (编译mysql用)
wget http://www.cmake.org/files/v2.8/cmake-2.8.9.tar.gz
tar -zxvf cmake-2.8.9.tar.gz
cd cmake-2.8.9
./configure &&make &&make install

//缺少gcc++ 要安装:yum install gcc-c++

开始编译安装Mysql

groupadd mysql
useradd -g mysql mysql                      // 添加mysql用户并加入mysql用户组
useradd -g mysql mysql -s /bin/false        // -s useradd命令的一个参数,使用者登入后使用的shell名称,此处指定/bin/false
mkdir -p /data/mysql                        // 创建Mysql数据目录
chown -R mysql:mysql /data/mysql            // 数据目录用户组

# 进入mysql源码包目录
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.27.tar.gz
wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.36.tar.gz
cd /data/soft/mysql/mysql-5.5.27

# 开始编译mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_TCP_PORT=3306 \
-DSYSCONFDIR=/etc \
-DINSTALL_SHAREDIR=share \
make
make install

注:重新运行配置,需要删除CMakeCache.txt文件

初始化Mysql

cp ./support-files/my-medium.cnf /etc/my.cnf
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql
/usr/local/mysql/bin/mysqld_safe --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql &
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

Mysql配置开机启动(配置完成记得给mysqld 赋执行权限)

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 2345 mysql on
chown mysql:mysql -R /usr/local/mysql/
service mysql start

Mysql 环境变量配置

vim /etc/profile        // 写入下面脚本
export PATH="$PATH:/usr/local/php/bin:/usr/local/mysql/bin"
source /etc/profile     // 环境变量生效

Mysql用户密码修改

/usr/local/mysql/bin/mysqladmin -u root -p password newpassword
1.例如你的 root用户现在没有密码,你希望的密码修改为123456,那么命令是:
mysqladmin -u root password 123456
2.如果你的root现在有密码了(123456),那么修改密码为abcdef的命令是:
mysqladmin -u root -p password abcdef
注意,命令回车后会问你旧密码,输入旧密码123456之后命令完成,密码修改成功。

配置Mysql数据库准许远程访问

# 登录Mysql
/usr/local/mysql/bin/mysql -h127.0.0.1 -uroot -p

# 进入Mysql表
use mysql;

# 查询表
select Host,User from user limit 10;

# 更新表
update user set Host='%' where User='root' and Host='localhost';

# 刷新Mysql权限
flush privileges;

Mysql 命令行登录

/usr/local/mysql/bin/mysql -h127.0.0.1 -uroot -p

查看Mysql进程/端口

netstat -an |grep 3306
ps -le | grep mysqld
ps aux | grep mysqld

SQL修改密码

update user set password=PASSWORD('woshishui') where user='root';
二、Nginx 编译安装

安装前提

[root@admin /]# yum -y install zlib zlib-devel openssl openssl-devel

安装Nginx# 安装pcre (支持nginx伪静态)

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
cd /usr/local/src
mkdir /usr/local/pcre                  // 创建安装目录
tar  zxvf pcre-8.30.tar.gz
cd pcre-8.30
./configure  --prefix=/usr/local/pcre  // 配置
make
make install

开始安装Nginx

[root@admin local]# groupadd  www  #添加www组
[root@admin local]# useradd -g  www www -s /bin/false  // 不允许www用户直接登录系统
[root@admin local]# cd /data/soft/
[root@admin local]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@admin local]# tar -zxvf nginx-1.6.2.tar.gz
[root@admin local]# cd nginx-1.6.2/
[root@admin nginx]# ./configure --prefix=/usr/local/nginx
[root@admin nginx]# make
[root@admin nginx]# make install

Nginx启动脚本

#!/bin/bash
# nginx     This shell script takes care of starting and stopping
#           nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest 
### END INIT INFO
#variables
NGINX_BIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
if [ $2 == 0 ]; then
echo -n $"$1 ${prog}:" && success && echo
else
echo -n $"$1 ${prog}:" && failure && echo
fi
}
#start nginx
function start {
rm -f ${NGINX_PID} 2>/dev/null
if [ -s ${NGINX_PID} ]; then
echo "nginx already running" 
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
${NGINX_BIN} -c ${NGINX_CONF} 
if_no start $?
        else
${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
if_no start $?
fi
fi
}
#stp nginx
function stop {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -QUIT
if_no stop $?
else
        if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
if_no stop 0
else
rm -f ${NGINX_PID} 2>/dev/null
kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
if_no stop $?
fi
fi
}
function restart {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_no restart $?
else
stop
sleep 1
start
fi
}
function status {
${NETSTAT} -tnpl | grep nginx | grep LISTEN
[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
${NGINX_BIN} -t
}
case $alter in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
configtest)
configtest
;;
*)
echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac

配置Nginx自启动脚本

[root@admin nginx]# chmod +x /etc/init.d/nginx
[root@admin nginx]# /etc/init.d/nginx start 或 service nginx start         // 启动nginx
[root@admin nginx]# /etc/init.d/nginx stop 或 service nginx stop          // 关闭nginx
[root@admin nginx]# /etc/init.d/nginx restart 或 service nginx restart       // 重启nginx

配置开机自动启动

chkconfig --add nginx
chkconfig --level 2345 nginx on
三、Php 编译安装

检查系统安装的php

[root@iZ23g4snm6gZ soft]# find -name php

PHP 编译依赖包安装

# 注意:freetype在生成验证码图片需要用,所以必须要安装的
[root@iZ23g4snm6gZ soft]# yum install openssl-devel  libxml2 libxml2-devel curl-devel  libevent
[root@iZ23g4snm6gZ soft]# yum install libpng libpng-devel libjpeg libjpeg-devel freetype-devel gd gd-devel 

# 源码包安装libiconv
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14/
./configure --prefix=/usr/local/libiconv
make
make install

# 源码包安装libiconv
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/usr/local/libmcrypt/
make
make install

开始编译PHP(Nginx)

./configure --prefix=/usr/local/php/ \
--with-config-file-path=/usr/local/php/etc/ \
--enable-fpm --with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-zlib \
--with-libxml-dir \
--enable-sockets \
--with-curl \
--with-jpeg-dir \
--with-png-dir \
--with-gd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir= \
--enable-gd-native-ttf \
--with-xmlrpc \
--with-openssl \
--with-mhash \
 --with-mcrypt=/usr/local/libmcrypt/ \
--with-pear \
--enable-mbstring \
--enable-sysvshm \
--enable-zip \
--with-mysql=/usr/local/mysql/ \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-mysql-sock \
--with-pdo-mysql \
--disable-fileinfo \

make
make install

设置PHP配置文件

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

# 拷贝模板文件为php-fpm配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf     // 编辑
user = www                             // 设置php-fpm运行账号为www  默认账号为nginx
group = www                            // 设置php-fpm运行组为www
pid = run/php-fpm.pid                  // 取消前面的分号
:wq!      

php-fpm启动脚本

#! /bin/sh
 
### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
 
prefix=/usr/local/php
exec_prefix=${prefix}
 
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
 
php_opts="--fpm-config $php_fpm_CONF"
 
wait_for_pid () {
    try=0
 
    while test $try -lt 35 ; do
 
        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;
 
            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac
 
        echo -n .
        try=`expr $try + 1`
        sleep 1
 
    done
 
}
 
case "$1" in
    start)
        echo -n "Starting php-fpm "
 
        $php_fpm_BIN $php_opts
 
        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi
 
        wait_for_pid created $php_fpm_PID
 
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    stop)
        echo -n "Gracefully shutting down php-fpm "
 
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
 
        kill -QUIT `cat $php_fpm_PID`
 
        wait_for_pid removed $php_fpm_PID
 
        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    force-quit)
        echo -n "Terminating php-fpm "
 
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
 
        kill -TERM `cat $php_fpm_PID`
 
        wait_for_pid removed $php_fpm_PID
 
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    restart)
        $0 stop
        $0 start
    ;;
 
    reload)
 
        echo -n "Reload service php-fpm "
 
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
 
        kill -USR2 `cat $php_fpm_PID`
 
        echo " done"
    ;;
 
    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload}"
        exit 1
    ;;
 
esac

设置php-fpm开机自启动

mv php-fpm /etc/init.d/               // 移动php-fpm脚本到init.d目录下
chmod a+x /etc/init.d/php-fpm         // 添加执行权限
chkconfig --add php-fpm               // 添加开机启动配置
chkconfig --level 2345 php-fpm on     // 配置开机启动权限级别
四、配置nginx支持php

打开nginx.conf,修改如下

vim /usr/local/nginx/conf/nginx.conf

nginx.conf文件内容:

# 首行user去掉注释,修改Nginx运行组为www www;
# 必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
user  www www;
worker_processes  1;

# 开启nginx错误日志
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    client_max_body_size 2m;

    #gzip  on;

    # 包含域名配置文件( 支持通配符)
    include vhost/*.conf;
}

配置 fastcgi.conf文件:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;    # 脚本文件请求的路径  
fastcgi_param  QUERY_STRING       $query_string;            # 请求的参数;如?app=123  
fastcgi_param  REQUEST_METHOD     $request_method;            # 请求的动作(GET,POST)  
fastcgi_param  CONTENT_TYPE       $content_type;             # 请求头中的Content-Type字段  
fastcgi_param  CONTENT_LENGTH     $content_length;             # 请求头中的Content-length字段。    
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;         # 脚本名称   
fastcgi_param  REQUEST_URI        $request_uri;              # 请求的地址不带参数  
fastcgi_param  DOCUMENT_URI       $document_uri;             # 与$uri相同。   
fastcgi_param  DOCUMENT_ROOT      $document_root;             # 网站的根目录。在server配置中root指令中指定的值   
fastcgi_param  SERVER_PROTOCOL    $server_protocol;             # 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。    
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;                # cgi 版本  
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;            # nginx 版本号,可修改、隐藏  
fastcgi_param  REMOTE_ADDR        $remote_addr;             # 客户端IP  
fastcgi_param  REMOTE_PORT        $remote_port;             # 客户端端口  
fastcgi_param  SERVER_ADDR        $server_addr;             # 服务器IP地址  
fastcgi_param  SERVER_PORT        $server_port;             # 服务器端口  
fastcgi_param  SERVER_NAME        $server_name;             # 服务器名,域名在server配置中指定的server_name
#fastcgi_param  PATH_INFO         $path_info;                # 可自定义变量  
# PHP only, required if PHP was built with --enable-force-cgi-redirect  
fastcgi_param  REDIRECT_STATUS    200;

配置虚拟主机公用配置文件server.conf:

# php文件访问配置
location ~ .*\.(php|php5)?$
{
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
}

# 静态文件缓存30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
    expires 30d;
    # access_log off;
}

# js,css文件缓存15个小时
location ~ .*\.(js|css)?$
{
    expires 15d;
    # access_log off;
}

配置虚拟主机文件 vhost/yphp.cn.conf:

server {
    listen 80;

    # 配置域名
    server_name  www.yphp.cn yphp.cn;

    # 配置网站目录
    root   /usr/local/nginx/html/yphp.cn;

    # 配置域名重定向
    if ($host != 'www.yphp.cn' ) {
        rewrite ^/(.*)$ http://www.yphp.cn/$1 permanent;
    }

    location / {

        # 配置rewrite
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }

        # include  /usr/local/nginx/html/yphp/.htaccess;
        # rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;

        # 配置默认访问文件
        index  index.php index.html index.htm;
    }

    # 包含虚拟主机公用配置文件
    include server.conf;
}

测试看看我们的辛苦是否有回报吧

/etc/init.d/nginx stop         # 停止nginx 服务
/etc/init.d/nginx start        # 启动nginx 服务

记得绑定下host文件,然后在浏览器输入我们配置的域名,yphp.cn

its over my baby

**

给程序员一个鼓励呗!

**

微信

支付宝

文章来源:segmentfault,作者:mmy123456。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:william.shi#ucloud.cn(邮箱中#请改为@)进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
后台-系统设置-扩展变量-手机广告位-内容正文底部

本文标签

配置 LNMP

推荐文章

热门标签

    热门文章 最新文章 文章云

站内导航

全站搜索