当前位置: 首页 > 后端技术 > PHP

LNMP环境搭建

时间:2023-03-30 00:23:01 PHP

A.软件环境Linuxubuntu16.04LTS,下载地址Nginxnginx-1.12.0,下载地址PHPphp-7.1.4,下载地址MySQLmysql-5.7.18,下载地址在运行所有命令之前,请执行apt-getupdate。因为使用源码包编译安装,所以需要c编译器。如果提示c编译器,请执行sudoapt-getinstallgcc命令安装c编译器。但是你可能会遇到c编译器cc还是找不到的情况,请执行:apt-getinstallbuild-essential编译安装需要的软件包,需要make依赖:sudoapt-getinstallmakeginx依赖环境包:pcrepcre-8.40,下载地址(安装后可能会出现找不到,apt-getinstalllibpcre3-dev,具体请看)pcre2pcre2-10.23,下载地址zLibzlib-1.2.11,下载地址php依赖环境包:libssl-dev,安装方式:apt-getinstalllibssl-devlibxml2-dev,安装方式:apt-getinstalllibxml2-devlibcurl4-gnutls-dev,安装方式:sudoapt-getinstalllibcurl4-gnutls-devlibzip-devinstall方法:aptinstalllibzip-devaptinstalllibonig5,如果没有,请手动搜索:aptsearchonigurumatryphpgd库依赖一一:libpng-dev(apt-getinstalllibpng-dev)libjpeg-dev(apt-getinstalllibjpeg-dev)libxpm-dev(apt-getinst都是libxpm-dev)freetype-官网下载,不用这么麻烦apt-getinstalllibfreetype6-dev另外,如果有.h等头文件丢失,请按照错误提示,如apt-getinstalllibfreetype*或*freetype*这个安装就解决了!系统升级到ubuntu17.07php,升级到php-7.1.8时,会报错:libcurlproblemfind/usr-iname'*curl*'//创建软连接(系统升级导致修改curl默认路径,找不到php.....)ln-s/usr/include/x86-linux-...../curl/usr/include/curlphp编译安装扩展phpize命令依赖的依赖包m4,安装方式:apt-getinstallm4autoconf,安装方式:apt-getinstallautoconfmysql依赖包:libaio1,安装方式:apt-getinstalllibaio1找不到libpcre.so.1错误,请手动链接:sudoln-s/usr/local/lib/libpcre.so.1/lib/libpcre.so.1B。安装Linux系统C.安装环境需要的依赖D.安装Nginxsudotar-zxvfnginx-1.12.0.tar.gzcdnginx-1.12.0sudo./configure#这个目录要提前创建--prefix=/usr/local/WebEnvironment/Nginx/Web-1.12.0/Nginx-1.12.0#支持tcp/udp负载均衡--with-streamsudomakesudomakeinstallsetnginx.conf文件//修改运行用户userroot;//修改日志文件路径error_loglogs/error.log;http{//增加上传文件大小限制client_max_body_size2048M;//增加nginx等待phpcgi响应的时间fastcgi_read_timeout3600s;//添加自定义配置文件路径(避免在默认配置文件中添加新数据)include'vhost/*.conf';}然后在vhost目录下,创建www.conf配置文件:server{server_nametest.com;听80;索引index.html;根/我自己/网络/测试;位置/{自动索引开启;}}E。测试Nginxcd/usr/local/WebEnvironment/Nginx/Web-1.12.0/Nginx-1.12.0/confsudogeditnginx.conf#修改配置文件,添加域名(详见windows下nginx.conf配置文件)sudogedit/etc/hosts#修改hosts文件,添加对应域名称cd../sbinsudo./nginx#就可以了!不要sudostart./nginx,会提示报错的浏览器打开域名查看结果。F.安装PHP-7.1.4tar-zxvfphp-7.1.4.tar.gzcdphp-7.1.4sudo./configure//php主文件所在目录--prefix=/usr/local/WebEnv/php-7.1.4//默认的配置文件在./lib目录下//注意这里是目录!!//请不要使用该编译选项,php安装完成后,请使用php--ini//查看配置文件路径,然后进行修改。即建议使用默认路径//--with-config-file-path=/usr/local/webEnv/php-7.1.4--with-mysqli=mysqlnd--with-pdo-mysql=mysqlnd--with-openssl--with-tsrm-pthreads--with-curl--with=swoole--with-zlib--with-webp--with-jpeg--with-xpm--with-zip--with-xsl--with-freetype--with-mysqli//注意以下四个选项,需要提前安装相关依赖!//最后,freetype选项请使用find/-iname'freetype*'查找具体位置//下面注释掉的php编译选项不再需要,php会自动查找//--with-png-dir=/usr/lib//--with-jpeg-dir=/usr/lib//--with-xmp-dir=/usr/lib//--with-freetype-dir=/usr/local/include/freetype2--enable-gd--enable-calendar--enable-mbstring--enable-ftp--enable-pcntl--enable-sysvmsg--enable-sysvsem--enable-sysvshm--enable-shmop--enable-sockets--enable-mysqlnd--enable-bcmath--enable-maintainer-zts--enable-fpmsudomakesudomakeinstall简化版命令(可直接运行):./configure--prefix=/myself/environment/php--with-mysqli=mysqlnd--with-pdo-mysql=mysqlnd--with-openssl--with-tsrm-pthreads--with-curl--with=swoole--with-zlib--with-webp--with-jpeg--with-xpm--with-zip--with-xsl--with-freetype--with-mysqli--enable-gd--enable-calendar--enable-mbstring--enable-ftp--enable-pcntl--enable-sysvmsg--enable-sysvsem--enable-sysvshm--enable-shmop--enable-sockets--enable-mysqlnd--enable-bcmath--enable-maintainer-zts--enable-fpm&&make&&makeinstall修改php.ini需要复制解压后的php.ini-productionphp压缩包到/usr/local/WebEnvironment/PHP-7.2.0/lib/php.ini//增加上传文件大小post_max_size=2048M//单个上传文件最大大小upload_max_filesize=10M//一次最大上传文件数max_file_uploads=200//设置扩展路径extension_dir="/usr/local/WebEnvironment/PHP-7.2.0/extensions"//添加自定义扩展extension=redis.so//php事件扩展extension=ev.soextension=gd.so//设置时区date.timezone=PRC//设置php最大内存memory_limit=512M//开启错误提示display_errors=OnbasicokG.安装MySQL-5.7.18以下方法不是从源码编译安装的,不能指定安装目录!如需指定安装目录,请查看mysql-8.x源码编译安装tar-zxvfmysql-5.7.18.tar.gzmvmysql-5.7.18/usr/local/mysqlcd/usr/local//它没有数据目录,需要自己创建(高版本已经有数据目录,不用再创建)sudomkdir-m755datacdbin//初始化数据目录的内容sudo./mysqld--initialize-insecure//启动,必须??以root身份启动sudo。/mysqld--user=root------------启动另一个终端-------------------------------//设置密码sudo./mysqladmin-urootpassword123456//登录测试mysql-uroot-p//配置文件(自己创建my.cnf,默认文件名,配置文件路径:etc目录,即可/etc,or/usr/local/mysql/etc)sudomkdir-m755/usr/local/mysql/etc//编辑配置文件sudogedit/usr/local/mysql/etc/my.cnf//配置文件内容(设置编码)[mysql]default-character-set=utf8[mysqld]character-set-server=utf8H。nginxaddincludevhost/*pathsetinthevirtualhostnginx.confcdconf/vhostsudovimvhost.conf//添加如下内容,保存server{server_nametest.com;根/var/网站/测试;索引index.phpindex.html;字符集utf-8;location/{//这里配置为单入口访问(比如你使用的PHP框架是Laravel,需要单入口Intersection)//这里的意思是://如果$uri是文件,直接返回//如果$uri/是目录,直接返回//最后一个参数表示重定向!//否则返回/index.php?$query_string;try_files$uri$uri//index.php?$query_string;}location~\.php(.*){//phphandler监听的tcp端口fastcgi_pass127.0.0.1/var/Website/TestOther;索引index.html;字符集utf-8;location/{}#静态资源处理#例如:http://test.com/test.jpg#他会从/data目录中获取,而不是从/var/Website/TestOther目录中获取#注意匹配顺序,先正则表达式,后前缀位置~\.(gif|jpg|png)${root/data;}}我。php-fpm解释参考文章cgi、fastcgi、php-cgi、php-fpm连接及区别配置文件路径://php-fpm配置文件etc/php-fpm.conf.default//进程池配置文件路径etc/php-fpm.d/www.conf.default//php.ini配置文件(从解压后的压缩包根目录复制重命名)etc/php.ini注意,php-fpm.conf配置文件是php-fpm程序的配置文件,而www.conf是php-fpm.conf配置文件的一部分(扩展名),只是被php隔开,它们是一个整体。这里的修改是www。conf:user=root#这里可以新建用户,也可以直接用root用户(为了省事,我就root用户)group=root#同上listen=127.0.0.1:9000#那里只能一个听(我被骗了最惨的!我以为我可以听多个!其实我不能!)J.建立一个软连接sudoln-s/path/to/php/sbin/php-fpm/bin/php-fpmsudoln-s/path/to/nginx/sbin/nginx/bin/nginxsudoln-s/path/to/mysql/bin/mysqld/bin/mysqldK.StarttestNginx//windows下启动nginx//linux下启动sudonginx#startsudonginx-sreload#重启(重新加载配置文件)sudonginx-sstop#快速停止sudonignx-squite#Gracefulstopsudonginx-sreopen#重新打开日志文件(仅在nginx启动时有效)php://指定启动的配置文件//-c指定php运行时配置文件//-y指定php-fpm运行时配置文件//-R可选,以root身份运行php-fpmsudophp-fpm-cphp.ini-yphp-fpm.conf-R//通常的启动方式应该如下:sudophp-fpm-Rmysqlsudomysqld--user=根