搜索
首页后端开发php教程PHP环境如何搭建?PHP环境搭建(详解)

php环境搭建的方法:首先通过命令“yum install wget”安装wget;然后安装Nginx,并编译依赖gcc环境;接着编译安装MySQL,并配置环境变量;最后通过install命令编译安装PHP,并启动“PHP-FPM”即可。

PHP环境如何搭建?PHP环境搭建(详解)

PHP环境如何搭建?本篇教程给大家带来的内容是PHP环境如何搭建?PHP环境搭建(详解)

按照文章的步骤操作,可以帮助你在CentOS6.*系统上搭建一个LNMP环境或者LAMP环境。这些环境可以作为服务器的线上运行环境。

相关推荐:    

●《2019年最好用的6个php环境搭建工具推荐

●《php环境配置教程: PHP开发环境集成安装与独立安装视频教程

● 《mac php环境搭建教程:mac php开发环境搭建和配置教程

作为一名PHP开发者,我们一定要懂得如何搭建PHP开发环境,目前主流的PHP开发环境组合是LAMP和LNMP,本文将介绍如何在CentOS上搭建LNMP开发环境。

目录:

一:准备工作

二:安装Nginx

三:安装MySQL

四:安装PHP

五:CentOS7开机/etc/rc.local不执行问题

一:准备工作

1.安装wget

wget 是一个从网络上自动下载文件的自由工具,支持通过 HTTP、HTTPS、FTP 三个最常见的TCP/IP协议下载,并可以可以使用HTTP代理。

sudo yum install wget

2.安装net-tools

最小化安装CentOS7时如果无法使用ifconfig命令,则需要安装net-tools,如果是安装的CentOS6版本则无需安装

sudo yum install net-tools

3.更新yum源

yum -y update

4.安装vim

sudo yum install vim

5.配置显示行号

vim ~/.vimrc
set nu #输入 set nu 后退出保存

二:安装Nginx

1.安装依赖

(1) 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装gcc-c++。

(2) PCRE是一个Perl库,中文"Perl兼容的正则表达式库"。安装Nginx是为了使Nginx支持具备URI重写功能的rewrite模块,如果不安装pcre库,则Nginx无法使用rewrite模块功能,Nginx的Rewrite模块功能几乎是企业应用必须。

(3) zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

(4) OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要安装 OpenSSL 库 。

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

说明: yum安装方式安装的pcre版本比较低,不过基本不影响使用

检查基础依赖包

上面的依赖安装完成后可以通过如下命令检查各个依赖安装是否成功

rpm -qa pcre pcre-devel
rpm -qa zlib zlib-devel
rpm -qa openssl openssl-devel
rpm -qa pcre pcre-devel

2.编译安装Nginx

# 这里我们把安装包都放到了/usr/src目录下,便于统一管理
cd /usr/src  #切换到软件包目录
wget https://nginx.org/download/nginx-1.14.1.tar.gz   #下载nginx源码包
useradd nginx -s /sbin/nologin -M   #创建nginx用户用于管理nginx程序
tar zxvf nginx-1.14.1.tar.gz  #解压nginx源码包

cd nginx-1.14.1

#预编译
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx-1.14.1 \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_stub_status_module

make && make install #编译 和 安装

cd /usr/local
ln -s nginx-1.14.1 nginx  #创建nginx的软链接

安装说明

--prefix=PATH    #设置安装路劲
--user=USER      #进程用户权限
--group=GROUP    #进程用户组权限
--with-http_stub_status_module   #激活状态信息
--with-http_ssl_module  #激活ssl功能

3.配置环境变量

vim /etc/profile
export PATH=/usr/local/nginx/sbin:$PATH
source /etc/profile

4.配置开机自启

vim /etc/rc.local
# Nginx开机自启
/usr/local/nginx/sbin/nginx &

5.Nginx常用命令

/usr/local/nginx/sbin/nginx -t # 检查Nginx配置语法是否有误
/usr/local/nginx/sbin/nginx  #启动
/usr/local/nginx/sbin/nginx -s stop  #立即停止
/usr/local/nginx/sbin/nginx -s quit  #平滑停止
/usr/local/nginx/sbin/nginx -s reload #重载配置
/usr/local/nginx/sbin/nginx -s reopen #重开日志

服务启动检查

可以通过该命令查询80端口被谁占用

lsof -i :80

如果无法识别该命令,需要安装lsof

sudo yum install lsof

6.关闭防火墙

CentOS6:
service iptables stop   临时关闭
chkconfig --level 2345 iptables off 永久关闭

CentOS7:
systemctl stop firewalld.service  #令关闭防火墙
systemctl disable firewalld.service  #关闭防火墙开机自启动
通过浏览器输入IP测试是否成功

三:安装MySQL

1.安装依赖

(1)cmake是新版MySQL的编译工具,必须安装

sudo yum install gcc gcc-c++ cmake ncurses-devel

如果你的系统是CentOS7,你还需要安装如下依赖

sudo yum install perl perl-devel autoconf

2.编译安装MySQL

useradd -s /sbin/nologin -M mysql  # 添加MySQL用户
wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.42.tar.gz

tar zxvf mysql-5.6.42.tar.gz

cd mysql-5.6.42

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.42 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.6.42/tmp/mysql.sock \
-DMYSQL_DATADIR=/usr/local/mysql-5.6.42/data \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_INNODB_MEMCACHED=1 \
-DWITH_DEBUG=OFF \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DENABLED_PROFILING=ON \
-DMYSQL_MAINTAINER_MODE=OFF \
-DMYSQL_TCP_PORT=3306

make && make install

3.MySQL配置

cd /usr/local/mysql-5.6.42
chown mysql.mysql /usr/local/mysql-5.6.42/data
mkdir tmp
chown mysql.mysql /usr/local/mysql-5.6.42/tmp
rm -f /etc/my.cnf
cp support-files/my-default.cnf /etc/my.cnf
scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql

4.加入守护进程

cd /usr/local
ln -s mysql-5.6.42 mysql
cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld

5.配置环境变量

vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile

6.启动MySQL

service mysqld start
mysql -u root -p #第一次登陆不需要密码,回车即可
set password for root@localhost = password('root');  #修改密码

四:安装PHP

1.安装依赖

sudo yum install gcc gcc-c++ zip unzip libxml2 libxml2-devel curl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel  zlib zlib-devel glibc glibc-devel glib2 glib2-devel gd-devel bzip2 bzip2-devel

2.编译安装PHP

cd /usr/src
wget http://hk1.php.net/get/php-7.2.12.tar.gz/from/this/mirror -O php-7.2.12.tar.gz

tar zxvf php-7.2.12.tar.gz
cd  php-7.2.12
./configure \
--prefix=/usr/local/php-7.2.12 \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-zlib \
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/usr/local/mysql/tmp/mysql.sock \
--with-gd \
--with-png-dir \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-openssl \
--with-curl \
--enable-bcmath \
--enable-mbstring \
--enable-static \
--enable-zip \
--enable-sockets \
--enable-xml


make && make install

3.PHP配置

cd /usr/local

ln -s php-7.2.12 php
cp  /usr/src/php-7.2.12/php.ini-development /usr/local/php-7.2.12/lib/php.ini
 
vim /usr/local/php/lib/php.ini
date.timezone = PRC  # 修改时区(大约在932行)

expose_php = Off  # 避免PHP信息暴露在http头中(大约369行)
 
display_errors = Off# 生产环境设置为off,开发环境就设置为On,便于调试
 说明:设置了dispaly_errors为off后,需要在php-fpm.conf中开启错误日志记录路径error_log = log/php-fpm.log
 
cd php 
cp etc/php-fpm.conf.default etc/php-fpm.conf

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf

# 管理PHP-FPM
vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
error_log = log/php-fpm.log #24行这个在php.ini设置display_errors = Off时启用

向进程发送信号,就可以完成进程管理
停止: kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
平滑停止: kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
重启:kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
重新打开日志:kill -USR1 `cat /usr/local/php/var/run/php-fpm.pid`

如果在编译PHP时指定了--with-mysql=mysqlnd和--with-pdo-mysql=mysqlnd的参数,那么在生产中可能会遇到socket连接问题,解决办法是在php.ini里加入命令: pdo_mysql.default_socket=/usr/local/mysql/tmp/mysql.sock

最好是在编译PHP的时候,指定mysql.socket的位置:
--with-mysql-sock=/usr/local/mysql/tmp/mysql.sock

4.配置环境变量

vim /etc/profile
export PATH=/usr/local/php/bin:$PATH
source /etc/profile

5.配置开机自启

vim /etc/rc.local
# PHP-FPM自动启动
/usr/local/php/sbin/php-fpm &

6.启动PHP-FPM

cd /usr/local/php
sbin/php-fpm # 启动PHP-FPM
ps -e | grep php-fpm

7.配置Nginx和PHP关联

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    
    #隐藏Nginx软件版本号
    server_tokens off;
    
    #激活tcp_nodelay功能,提高I/O性能
    tcp_nodelay on;

    # 设置读取客户端请求头数据的超时时间。此处的数值为15,其单位是秒,为经验参考值
    client_header_timeout 15;

    # 设置读取客户端请求体的超时时间
    client_body_timeout 15;

    # 指定响应客户端的超时时间
    send_timeout 25;

    # 上传文件大小限制
    client_max_body_size 8m;
    
    #压缩配置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/css text/xml text/plain application/javascript;
    gzip_vary on;
    #include extra/gzip.config;

    #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;
    keepalive_timeout  65;

    server {
         listen       80;
         server_name  www.nginx.com;
         root html/blog/public;
         #access_log  logs/host.access.log  main;

         location / {
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php/$1 last;
            }
         }

         #error_page  404              /404.html;

         # redirect server error pages to the static page /50x.html
         #
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
            root   html;
         }

         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
         #
         location ~ \.php {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
         }
    }
}

五:CentOS7开机/etc/rc.local不执行问题

CentOS7中,默认开机不再执行/etc/rc.local,查询/etc/rc.local里的内容可以发现如下描述:

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In constrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

翻译过来就是

#这个文件是为了兼容性的问题而添加的。
#
#强烈建议创建自己的systemd服务或udev规则来在开机时运行脚本而不是使用这个文件。
#
#与以前的版本引导时的并行执行相比较,这个脚本将不会在其他所有的服务后执行。
#
#请记住,你必须执行“chmod +x /etc/rc.d/rc.local”来确保确保这个脚本在引导时执行。

所以要解决开机不执行问题,只需要执行下面的命令然后重启服务器。

chmod +x /etc/rc.d/rc.local

完成上面这些步骤,一个LNMP环境就配好啦,重启服务器即可。

以上是PHP环境如何搭建?PHP环境搭建(详解)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP电子邮件:分步发送指南PHP电子邮件:分步发送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自动化notifications andMarketingCampaigns.1)设置设置yourphpenvironcormentswironmentswithaweberswithawebserverserverserverandphp,确保themailfunctionisenabled.2)useabasicscruct

如何通过PHP发送电子邮件:示例和代码如何通过PHP发送电子邮件:示例和代码May 09, 2025 am 12:13 AM

发送电子邮件的最佳方法是使用PHPMailer库。1)使用mail()函数简单但不可靠,可能导致邮件进入垃圾邮件或无法送达。2)PHPMailer提供更好的控制和可靠性,支持HTML邮件、附件和SMTP认证。3)确保正确配置SMTP设置并使用加密(如STARTTLS或SSL/TLS)以增强安全性。4)对于大量邮件,考虑使用邮件队列系统来优化性能。

高级PHP电子邮件:自定义标题和功能高级PHP电子邮件:自定义标题和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP发送电子邮件的指南使用PHP和SMTP发送电子邮件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP发送邮件可以通过PHPMailer库实现。1)安装并配置PHPMailer,2)设置SMTP服务器细节,3)定义邮件内容,4)发送邮件并处理错误。使用此方法可以确保邮件的可靠性和安全性。

使用PHP发送电子邮件的最佳方法是什么?使用PHP发送电子邮件的最佳方法是什么?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依赖注入的最佳实践PHP中依赖注入的最佳实践May 08, 2025 am 12:21 AM

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

PHP性能调整技巧和技巧PHP性能调整技巧和技巧May 08, 2025 am 12:20 AM

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

PHP电子邮件安全性:发送电子邮件的最佳实践PHP电子邮件安全性:发送电子邮件的最佳实践May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。