CentOS7搭建LNMP环境

本文详细介绍了在Linux环境下安装配置PHP、Nginx和MySQL的全过程,包括下载、编译、配置环境变量、修改配置文件、启动服务等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装PHP

cd /usr/local

1.下载:wget file.fengyumeng.com/php-7.2.20.tar.gz

2.解压:tar -zxvf php-7.2.20.tar.gz

3.安装依赖:yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel

4.安全必要依赖:

yum -y install gcc
yum -y install gcc-c++
yum -y install libxslt-devel*
yum -y install mod_ssl
yum -y install libtool-ltdl*
yum -y install perl* yum -y install autoconf

5.进入安装目录:cd /usr/local/php-7.2.20/

6.配置:

./configure --prefix=/usr/local/php7 --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-jpeg-dir --with-freetype-dir --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath -enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-pcntl --with-curl --with-fpm-user=nginx --enable-ftp --enable-session --enable-xml --without-pear --disable-phar --with-config-file-path=/usr/local/php7/etc/php.ini 

7.编译:make 如若编译出错可以查看 http://www.cnblogs.com/sweetXiaoma/p/5855732.html

8.安装:make install

9.添加环境变量:vim /etc/profile

在最后一行加入

PATH=$PATH:/usr/local/php7/bin
export PATH

10.生效配置:source /etc/profile

11.查看php版本:php -v

12.生成必要文件:

cp php.ini-production /usr/local/php7/etc/php.ini
cp sapi/fpm/php-fpm /usr/local/php7/etc/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

13.编辑php.ini: vim /usr/local/php7/etc/php.ini

date.timezone = PRC    //设置时区
file_uploads = On    //是否允许上传
upload_tmp_dir = /tmp    //上传临时目录
max_file_uploads = 20    //单个请求最多上传数量
upload_max_filesize = 10M    //允许上传文件大小
post_max_size = 20M    //允许post传输最大值(这个必须比upload_max_filezise大)
memory_limit = 128M    //设置脚本最大使用内存
error_reporting=E_ALL    //输出错误信息
error_log = /var/log/php.log    //错误日志路径

14.创建日志文件:

touch /var/log/php.log
chmod 755 /var/log/php.log

安装Nginx

cd /usr/local

1.下载Nginx:wget https://nginx.org/download/nginx-1.16.0.tar.gz

2.解压:tar -zxvf nginx-1.16.0.tar.gz

3.配置:./configure --with-http_stub_status_module --with-http_ssl_module

4.安装:make && make install

5.修改配置文件:vim /usr/local/nginx/conf/nginx.conf

server {
    listen       80;
    server_name localhost ;
    root  /mnt/www/php;
    location / {
            if (!-e $request_filename) {
                 rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                 rewrite ^(.*)$ /index.php?s=$1 last;
             }
        index  index.html index.htm index.php;
    }
    location ~ \.php$ {
        root           /var/www/abc;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

6.配置php-fpm:vim /usr/local/php7/etc/php-fpm.d/www.conf

把里面的user group 两行 改为nobody 或者是系统中存在的用户

user = nobody

group = nobody

7.启动php-fpm,载入php.ini

/usr/local/php7/sbin/php-fpm -c /usr/local/php7/etc/php.ini

8.编写服务脚本:vim /etc/init.d/nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

9.设置权限:chmod 755 /etc/init.d/nginx

10.加入开启自启:vi /etc/rc.local

11.在末行加入:/usr/local/nginx/sbin/nginx

12.服务命令启动:/etc/init.d/nginx start

13.加入开机启动:chkconfig nginx on

nginx 服务命令:

查看状态:systemctl status nginx 
启动nginx:systemctl start nginx
停止nginx:systemctl stop nginx
平滑重启nginx:systemctl reload nginx
重启nginx:systemctl restart nginx

安装MySQL

1.安装mysql源:yum localinstall -y http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

2.安装mysql:yum install mysql-community-server

3.启动mysql:systemctl start mysqld

4.获取初始密码:grep 'temporary password' /var/log/mysqld.log

5.连接mysql:mysql -uroot -p

6.修改密码:SET PASSWORD = PASSWORD('123##456');

7.开放远程连接:

use mysql;

update user set host = '%' where user = 'root';

8.创建其它访问账号:create user 'useraa'@'%' IDENTIFIED BY '123##456';

9.为账号开启所有权限:grant all on *.* to useraa;

10.刷新权限:flush privileges;

强制重启重启php-fpm:

1、先全部杀死php-fpm进程:

killall php-fpm

2、启动php-fpm进程:

/usr/local/php/sbin/php-fpm &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值