Linux的PHP开发环境快速搭建

摘要:
安装环境为LNMP:1。安装MySQL非常简单。如果我使用Ubuntu,我使用apt源代码,下载deb文件,然后按照新安装文档的顺序:a.添加apt库,b.更新apt库。c.安装,d.运行MySQL下载:https://dev.mysql.com/downloads/repo/apt/文件:https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#

搭建的环境是LNMP:

1、安装MySQL

这个非常简单我用的是Ubuntu那么就用apt源,下载deb文件然后按照全新安装文档按顺序:a.加入apt库  b.更新apt库 c.安装 d.运行MySQL

下载:

https://dev.mysql.com/downloads/repo/apt/

文档:

https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#apt-repo-fresh-install

2、PHP

这里开启php-fpm,监听9000端口。

相关文档:

http://php.net/manual/zh/install.unix.nginx.php

a. 下载

https://www.php.net/downloads.php
wget https://www.php.net/distributions/php-7.1.33.tar.gz

任意选择一个镜像下载到本地或者获取到下载地址然后wget下载到本地

b.解压、编译、安装

tar zxf php-x.x.x
cd ../php-x.x.x
./configure --prefix=/usr/local/php --enable-fpm --enable-pdo --with-pdo-mysql --enable-mysqlnd --with-mysqli --with-openssl
make
sudo make install

有精简控的一定加上--prefix,这样安装目录才会在那里

其中按顺序执行下来会遇到的问题有pcre、zlib、libxml2不存在的问题,那么直接百度进入官网获取最新版本的tar.gz格式安装包然后解压编译安装。

swoole 的入门手册

https://linkeddestiny.gitbooks.io/easy-swoole/content/book/chapter01/install.html
Linux的PHP开发环境快速搭建第1张Linux的PHP开发环境快速搭建第2张
./configure --prefix=/usr/local/php 
--with-config-file-path=/etc/php 
--enable-fpm 
--enable-pcntl 
--enable-mysqlnd 
--enable-opcache 
--enable-sockets 
--enable-sysvmsg 
--enable-sysvsem 
--enable-sysvshm 
--enable-shmop 
--enable-zip 
--enable-soap 
--enable-xml 
--enable-mbstring 
--disable-rpath 
--disable-debug 
--disable-fileinfo 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd 
--with-pcre-regex 
--with-iconv 
--with-zlib 
--with-mcrypt 
--with-gd 
--with-openssl 
--with-mhash 
--with-xmlrpc 
--with-curl 
--with-imap-ssl
View Code

c、安装完毕以后配置文件(官方文档搬砖过来的),每一行都不能忘记哦

sudo cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/php/bin

小插曲:防止文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击

vim /usr/local/php/lib/php.ini
修改参数为:cgi.fix_pathinfo=0

由于本人对vim也不熟悉所以建议sudo atom 或者sudo sublime之类以图形界面软件打开.

d、下面和PHP手册不一样的是:(以下功能是让fpm读取配置PHP-FPM用户组和用户并开启监听9000端口)

实际上手册所说/usr/local/etc/php-fpm.conf根本没有用户组配置选项,自己手动加上又会报告文件找不到,甚是郁闷,应该这样树立

创建web用户:

groupadd www-data
useradd -g www-data www-data

打开php-fpm.conf

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

找到最下面那一行:

include=NONEl/etc/php-fpm.d/*.conf

将NONE改为真实路径:

include=/usr/local/php/etc/php-fpm.d/*.conf

然后在这个正则匹配配置文件的加上用户组和用户信息:

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

查找到用户设置跟改内容为以下:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www-data
group = www-data

开启FPM

/usr/local/bin/php-fpm

e、检查是否成功:

netstat -tln | grep 9000

如果看到TCP 9000 LISTEN则表示配置成功如果无任何输出表示9000端口未监听,请重试

3、安装Nginx

Nginx中文文档呢让我踩坑了

http://www.nginx.cn/install
http://www.nginx.cn/doc/setup/nginx-ubuntu.html

这两个都是版本太旧,PHP5什么的或者参数特别多安装总有各种问题,而且不喜欢apt去安装,起码版本选择上没那么自由,删除也要用apt去删除所以我用最简单的方式安装就是下载、编译、安装

下载

http://nginx.org/en/download.html

选一个喜欢的版本下载完了

tar -zxvf 
cd 
./configure --prefix=/usr/local/nginx
make 
make install

如果在configure时候提示一些zlib之类的软件不存在百度下载tar包解压安装便是

安装目录解答:

安装完了以后在 /usr/local/nginx

配置文件:/usr/local/nginx/conf/nginx.conf

虚拟主机文件目录:/usr/local/nginx/html

执行文件:/usr/local/nginx/sbin/nginx

配置文件需要做到的就是 a.index.html后面加index.php   b.符合.php规则的交给9000端口

端口用默认nginx:80 php:9000 虚拟主机目录用默认的 /usr/local/nginx/html

一共配置了两个地方:

location / {
            root   html;
            index  index.html index.php index.htm;
        }
#location ~ .php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
#    include        fastcgi_params;
#}

好啦,现在去执行目录

sudo ./nginx -s reopen

 启动nginx

html目录新建test.php浏览器输入localhost/text.php就可以看到配置成功了.

 Linux的PHP开发环境快速搭建第3张

PHP扩展安装:

参考手册

http://php.net/manual/zh/install.pecl.phpize.php

执行流程是:进入编译时用的PHP源码包的ext在进入相关扩展目录, phpize生成configure文件, ./configure ,  make && make install

cd /home/username/php7.0.29/ext
cd curl
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

可能会出现autoconf出错情况,如果是ubuntu直接运行就可以了,然后去lib.php/ini将扩展注释去除就可以咯:

sudo apt-get install autoconf

Linux 简单和常用的指令:

1、根目录下查找httpd.conf文件:

find / -name httpd.conf

2、显示/usr/src目录下的文件(包含子目录)包含magic的行,这个查找的可是文件的内容:

grep -r magic /usr/src

 3、VIM编辑器查找内容

尾行模式:/content Enter

 4、php-fpm的进程关闭

sudo pkill php-fpm

 5、MySQLI安装

./configure –with-php-config=/usr/local/php/bin/php-config –with-mysqli=/usr/bin/mysql_config

php_config找不到

sudo apt-get install libmysqlclient-dev  

 之后遇到make错误,mysqli.lo不存在,是因为某个.h文件未找到导致编译失败图示:

Linux的PHP开发环境快速搭建第4张

解决方案:

https://www.cnblogs.com/xiaoqian1993/p/6277771.html

6、ubuntu安装apt install资源占用

Could not get lock /var/lib/dpkg/lock!
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

 7、简单的定时任务

ubuntu 设定定时器任务:

1、
    将ubuntu中crontab的编译器切换到VIM
    export EDITOR=vim  
    修改后最好重启一下crontab
    /etc/init.d/cron stop  
    /etc/init.d/cron start 

2、
    设定每一分钟向/home/hello.txt文本追加一个hello
    创建tesh.sh内容:
    echo hello >> /home/hello.txt
    创建文件hello.txt(注意所属用户、所属组、其他用户)的读写执行权限的分配.
    将.sh加入定时任务
    命令行输入 
    crontab -e
    编辑文本内容为
    */1 * * * * sh /home/test.sh

 linux添加环境变量:

由于linux环境变量值中/usr/local/php并不属于,/usr/local/bin里面的倒是可以全局访问的,现在将php加入全局变量。

sudo vim /etc/profile

//加入mysql、PHP的执行文件所在目录

PATH=$PATH:/usr/local/php/bin:/usr/local/mysql/bin
export PATH

//两行代码加到末尾然后执行以下指令使其生效
source /etc/profile

 或者添加快捷方式形式:

ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config

nginx.conf | laravel

Linux的PHP开发环境快速搭建第5张Linux的PHP开发环境快速搭建第6张
#user  www-data;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    #gzip  on;
    #
    server {
        listen       8080;
        server_name  localhost;

        index index.html index.htm index.php;

        location / {
            root   /home/www/laravel/public;
            autoindex on;
            try_files $uri $uri/ /index.php?$query_string;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
            root           /home/www/laravel/public;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    server {
        listen       80;
        server_name  localhost;

        index index.html index.htm index.php;

        location / {
            root   /home/www;
            autoindex on;
            try_files $uri $uri/ /index.php?$query_string;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
            root           /home/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }


}
View Code

 composer安装

https://pkg.phpcomposer.com/#how-to-install-composer

免责声明:文章转载自《Linux的PHP开发环境快速搭建》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS图片加载框架-SDWebImage解读ant design pro (十五)advanced 使用 API 文档工具下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Jetson AGX Xavier/Ubuntu测试SSD的读写速度

我是在Jetson AGX Xavier上测试的,Ubuntu上应该也可以用。 1. 查看SSD df -h /dev/nvme0n1p1是我要测试的硬盘。 2. 读速度 sudo hdparm -tT /dev/nvme0n1p1 -t 执行设备读取的时间 -T 执行缓存读取的时间 多测几次会准一些 for i in 1 2 3; do sudo h...

解决Class 'swoole_server' not found

1.看下cli模式是否可以正常工作,命令行下运行 php -r "echo php_sapi_name();" 这条命令就是在cli模式运行php语句,php -r就是run一条php命令的意思,php_sapi_name()判断当前执行的php是什么模式下,执行成功在屏幕上输出 cli,不成功说明你的php cli模式有问题。 2.查看当前cli已支...

nginx 配置详解(新手必看)

内容来源:http://blog.csdn.net/david_xtd/article/details/16967837 译者注:不知道其他开发者是否和我一样,参与或者写了很多Web项目,但是却没有真正的去完整的部署应用,很多时候都是交给ops即运维的同学帮忙来做。而作为一个有节操的开发者,我认为了解一些服务器方面的知识是很有必要的,读了这篇文章之后,自己...

PHP连接SQLSERVER及中文乱码问题

PHP连接SQLSERVER 1、PHP 5.3及以后版本不再支持mssql模块,应使用SQLSRV或PDO_SQLSRV; 2、下载 PHP 驱动程序。 http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx 内含Windows平台各种版本扩展文件,支持SQL2005及以上版本 3、复制所需文件如:...

linux命令集合

查找当前路径文件内容: find ./ -name "*" | xargs grep "kaka" 查找文件位置: locate ** 查看系统磁盘空间: df -h 查看某目录占用空间: du -h ./ 查看某文件夹内占用磁盘空间: du -sh * 查看指定文件夹占用磁盘空间:du -sh /company 查看文件夹内文件的个数: find ./...

phpCB 批量格式化php文件 经典方法

发现phpCB整理php文档非常好,但有个缺点是不能批量处理,使用过程中发现phpCB是一个CMD程 序。于是想到php的system函数调用cmd。将phpCB放到 系统文件夹 system下。 下面是phpCB批量转换的php程序: <? header ( "Content-type: text/html; charset=gb2312" ) ;...