LNMP环境配置

Linux + Nginx + MySQL + PHP
PHP是一种脚本语言,当前中国乃至世界上使用php语言开发网站非常普遍
Nginx是一个web服务软件,和apache是一类软件,目前使用nginx的企业越来越多
MySQL是当前最为流行的小型关系型数据库
LNMP就是一个支持解析php程序的环境


如果你的系统之前装过LAMP,先停止mysql:

# /etc/init.d/mysqld stop

然后删除 mysql 目录, 不删除应该也可以,要做实验试试。

# rm -rf /usr/local/mysql/

如果想彻底的删除mysq 还要删 /etc/init.d/mysqld 和 /etc/my.cnf 这两个文件。

这里我们是配置LNMP就不删除这俩个文件了。

解压:

# tar zxvf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz

解压的目录移到 /usr/local 下并改名为 mysql:

# mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql

这里把以前生成的 mysql 数据删除掉:

# rm -rf /data/mysql/*

然后初始化数据库:

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

看看系统服务项里有没有mysql ,没有使用 chkconfig 命令把 mysql 加入进去,然后启动:

# service mysqld start

如果你之前没有配置过LAMP就,在编译PHP的时候要 # make clean 下 ,因为编译PHP的时候你 make 和 make install 的时候生成了很多文件,避免你在编译LNMP的时候搞乱,所以要 make clean 下。


安装MySQL

1.下载mysql到/usr/local/src/:

# cd /usr/local/src/ 
# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.26-linux-glibc2.5-i686.tar.gz
2.解压:

# tar zxvf mysql-5.6.26-linux-glibc2.5-i686.tar.gz

3.把解压完的数据移动到/usr/local/mysql:

# mv mysql-5.6.26-linux-glibc2.5-i686.tar.gz /usr/local/mysql 
4.建立mysql用户:
# useradd -s /sbin/nologin mysql
5.初始化数据库:
# cd /usr/local/mysql
# mkdir -p /data/mysql 
# chown -R mysql:mysql /data/mysql 
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 
这一步会报错:
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
缺少libaio-devel 库安装就好。
# yum install -y libaio-devel
 --user 定义数据库的所属主,--datadir 定义数据库安装到哪里,建议放到大空间的分区上,这个目录需要自行创建。这一步骤很关键,如果你看到两个 “OK” 说明执行正确,否则请仔细查看错误信息。
6.拷贝配置文件:
# cp support-files/my-default.cnf /etc/my.cnf
7.拷贝启动脚本文件并修改其属性:
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
8.修改启动脚本,需要修改的地方有 “datadir=/data/mysql” (前面初始化数据库时定义的目录)
# vim /etc/init.d/mysqld   
9.把启动脚本加入系统服务项,并设定开机启动,启动mysql
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start
如果启动不了,请到 /data/mysql/ 下查看错误日志,这个日志通常是主机名.err. 检查mysql是否启动的命令为:
# ps aux |grep mysqld

安装PHP

建议你使用5.4或者5.5版本,php官方下载地址: http://www.php.net/downloads.php

1.下载php:

# cd /usr/local/src/

# wget http://am1.php.net/distributions/php-5.5.30.tar.gz

2.解压:

# tar zxvf php-5.5.30.tar.gz

3.创建相关用户:

# useradd -s /sbin/nologin php-fpm

4.配置编译参数:

# cd php-5.5.30

./configure --prefix=/usr/local/php   --with-config-file-path=/usr/local/php/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  --enable-zend-multibyte   --disable-ipv6   --with-pear   --with-curl 

在编译过程中你会遇到错误就是缺少库文件,如果出现不适下面的错误,请看我的 LAMP 的环境配置文章。

configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

解决办法:

# yum install -y libcurl-devel

5.编译PHP:

# make 

在编译的过程中遇到了:

/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: *** [sapi/fpm/php-fpm] 错误 1

解决办法:

# yum install -y libtool-ltdl-devel

6.安装:

# make install

以上每一个步骤,如果没有完全执行正确,那么下一步是无法进行的,用 echo $? 查看结果是否为 0 ,如果不是,就是没有执行正确。

7.修改配置文件:

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

# vim /usr/local/php/etc/php-fpm.conf

添加以下内容:

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
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 
保存配置文件后,检验配置文件是否正确的方法如下:
# /usr/local/php/sbin/php-fpm -t
如果显示 test is successful ,则说明配置没有问题,否则就要根据提示检查配置文件。
也可以在增加一个:
[abc.com]
listen = /tmp/php-abc.com.sock  //这个地方监听的地址不能一样
user = nobody //这个地方也可以修改
group = nobody //还有这个地方
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

8.启动 php-fpm :
先拷贝启动脚本:
# cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod 755 /etc/init.d/php-fpm

启动:

# service php-fpm start

9.查看 php-fpm 是否启动

# ps aux |grep php-fpm


安装nginx

1.下载和解压Nginx :

# cd //usr/local/src

# wget http://nginx.org/download/nginx-1.4.4.tar.gz

# tar zxvf nginx-1.4.4.tar.gz

2.配置编译选项:

# cd nginx-1.4.4

# ./configure   --prefix=/usr/local/nginx   --with-http_realip_module   --with-http_sub_module  --with-http_gzip_static_module   --with-http_stub_status_module  --with-pcre

3.编译和安装:

# make

# make install

4.编写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 /etc/init.d/nginx

然后设置开机启动:

# chkconfig nginx on


4.更改Nginx的配置文件:

因为要更改的地方过多,就重新写了个。

先清空配置文件:

# > /usr/local/nginx/conf/nginx.conf

编辑配置文件:

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

加入:

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;
    }


}


}

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

# /usr/local/nginx/sbin/nginx -t

如果显示:

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

则说明配置正确,否则要根据错误提示修改配置文件。

5.启动Nginx:

# /etc/init.d/nginx start

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

# ps aux |grep nginx

6.测试是否正确解析PHP :

# vim /usr/local/nginx/html/1.php

写入以下内容:

<?php
echo " Hello word! ";
?>

保存,执行如下命令测试:

# curl localhost/1.php

 Hello word!

显示上面的内容说明PHP解析正确。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 CentOS 7 上配置 LNMP 环境的步骤: 1. 安装 Nginx 使用 yum 安装 Nginx,命令如下: ```bash sudo yum install nginx ``` 安装完成后,启动 Nginx 服务,命令如下: ```bash sudo systemctl start nginx ``` 可以使用以下命令检查 Nginx 是否已经启动: ```bash sudo systemctl status nginx ``` 2. 安装 MySQL 使用 yum 安装 MySQL,命令如下: ```bash sudo yum install mysql-server mysql ``` 安装完成后,启动 MySQL 服务,命令如下: ```bash sudo systemctl start mysqld ``` 可以使用以下命令检查 MySQL 是否已经启动: ```bash sudo systemctl status mysqld ``` 3. 安装 PHP 使用 yum 安装 PHP,命令如下: ```bash sudo yum install php php-mysql ``` 安装完成后,需要重启 Nginx 服务,使其能够识别 PHP,命令如下: ```bash sudo systemctl restart nginx ``` 4. 配置 Nginx 编辑 Nginx 配置文件,命令如下: ```bash sudo vi /etc/nginx/nginx.conf ``` 在 `http` 代码块内添加以下内容: ```bash server { listen 80; server_name example.com; # 修改成你自己的域名 root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` 保存并退出文件,然后重新启动 Nginx 服务,使其识别新的配置: ```bash sudo systemctl restart nginx ``` 5. 配置 MySQL 使用以下命令安全地配置 MySQL: ```bash sudo mysql_secure_installation ``` 按照提示输入一个 root 密码,并回答一些其他问题。 6. 配置 PHP 编辑 PHP 配置文件,命令如下: ```bash sudo vi /etc/php.ini ``` 找到以下行: ```bash ;date.timezone = ``` 将其修改为以下内容,将时区修改为你所在的时区: ```bash date.timezone = Asia/Shanghai ``` 保存并退出文件,然后重新启动 PHP-FPM 服务: ```bash sudo systemctl restart php-fpm ``` 至此,LNMP 环境配置完成。你可以将你的网站文件放到 `/var/www/html` 目录下,并通过浏览器访问你的网站。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值