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

安装LNMP开发环境

时间:2023-03-29 20:56:33 PHP

首发于范浩博科学院本文主要介绍LNMP开发环境的安装,并列举了PhpStorm的安装过程。源码包放在/usr/src,软件安装在/usr/local。CentOS安装到CentOS官网下载CentOS6.9Min版镜像文件。新建虚拟机并安装,但注意在新建虚拟机向导设置中,必须选择稍后安装操作系统,完成虚拟机安装向导后,指定系统镜像(CD/DVD)的地址项)通过编辑虚拟机设置。Min版本安装后,默认不启用网卡。使用以下方法启用网卡。$vi/etc/sysconfig/network-scripts/ifcfg-eth0配置信息如下:DEVICE=eth0HWADDR=00:0C:29:7A:CF:56TYPE=EthernetUUID=216048ec-c974-427e-8b57-5a4c9fe6733e#是否自动开机,将no改为yesONBOOT=noNM_CONTROLLED=yesBOOTPROTO=dhcp配置,然后重启,否则无法连接网络。配置1)安装epel,解决第三方扩展依赖问题$yuminstallepel-release2)更新系统$yum-yupdate3)安装必要工具#查看流量$yum-yinstalliptraf#常用工具$yum-yinstallwgetcurllrzszvimunzipzip#GCC$yum-yinstallgccgcc-c++#cmake$yum-yinstallcmake4)同步时钟$yum-yinstallntpdatentp$ntpdatetime.windows.com&&hwclock-w在计划任务中写入以下内容:0301***/usr/sbin/ntpdate-utime.windows.com>/var/log/ntpdate.log&5)修改DNS设置$vim/etc/resolv.conf#添加primaryDNSnameserver8.8.8.8#AddDNSnameserver8.8.4.46)modifythelanguage查看系统语言:#如果是zh_CN就是中文,如果是en_US就是英文$echo$LANG#查看系统语言包$locale#下载中文语言包$yum-ygroupinstallchinese-support通过编辑/etc/sysconfig/i18n配置文件进行修改。7)桌面为了方便后续开发,这里安装了GNOME桌面。$yumgroupinstall"XWindowSystem""Desktop""DesktopPlatform""Fonts""ChineseSupport[zh]"#修改默认启动方式$vim/etc/inittabid:5:initdefault:PHP下载源码并解压PHP官方地址下载源码包。$cd/usr/src$wgethttp://am1.php.net/distributions/php-5.6.30.tar.gz$tarzxvfphp-5.6.30.tar.gz$cdphp-5.6.30安装依赖您可以通过./configure脚本检查依赖安装。$yum-yinstallgd-develpcre-devellibxml2-devellibjpeg-devellibpng-devellibevent-devellibtool*autoconf*freetype*libstd*gcc44*ncurse*bison*openssl*libcurl*libcurl*libmcrypt*当使用yum安装时libmcrypt可能找不到安装源,这里是libmcrypt的源码安装方法。$wgetftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.ta??r.gz$tarzxflibmcrypt-2.5.7.ta??r.gz$cdlibmcrypt-2.5.7$。/configure$make&&makeinstall编译安装./configure--prefix=/usr/local/php\--with-mysql=mysqlnd\--with-mysqli=mysqlnd\--with-pdo-mysql=mysqlnd\--with-iconv-dir\--with-freetype-dir\--with-jpeg-dir\--with-png-dir\--with-zlib\--with-libxml-dir\--enable-xml\--disable-rpath\--enable-bcmath\--enable-shmop\--enable-sysvsem\--enable-sysvshm\--enable-sysvmsg\--enable-inline-optimization\--with-curl\--with-mcrypt\--enable-mbregex\--enable-fpm\--enable-mbstring\--with-gd\--enable-gd-native-ttf\--with-openssl\--with-mhash\--enable-pcntl\--enable-sockets\--with-xmlrpc\--enable-zip\--enable-soap\--enable-ftp\--without-pear\--enable-opcache$make&&makeinstall如果在编译过程中出现如下错误:configure:error:Don'tknowhowtodefinestructflockonthissystem,set-enable-opcache=no采用如下办法解决:$vim/etc/ld.so.conf.d/local.conf#在/usr/local/lib文件中添加如下内容#运行后重新编译PHP$ldconfig配置1)复制并修改php.ini配置文件$cp./php.ini-development/usr/local/php/lib/php.ini建议修改开发环境配置如下:short_open_tag=Onoutput_buffering=4096max_execution_time=60#错误显示和级别error_reporting=E_ALLdisplay_errors=Ondisplay_startup_errors=Onlog_errors=On#把PHP的错误记录记录在syslog或者指定的文件中(注意目录权限)error_log=syslog#setthedefaulttimezonedate.timezone=PRC2)php-fpm.conf复制并修改php-fpm.conf配置文件。cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf建议修改配置如下:[global]#重要,一定要记录notice,及时发现异常情况log_level=notice#重要,每个子进程可以打开的文件描述符数量的上限(系统默认是1024),一定要尽量增加rlimit_files=60000[www]#重要,设置fpm运行状态查看地址pm.status_path=/status#fpm生存状态检测地址ping.path=/ping#用于检测fpm进程是否正常运行ping.response=pong#子进程处理指定数量的请求pm.max_requests=5000#定义慢日志文件位置slowlog=var/log/slow.log#慢请求时间的上下限,超过这个值就会记录在日志文件中request_slowlog_timeout=1s#单个脚本运行超时时间,在fpm模式下,max_exphp.ini中的ecution_time配置无效,必须指定request_terminate_timeout=30s#抓取php程序的错误报告,发送给客户端(nginx)。如果不配置这个,php通过nginx访问会报错,在nginx响应中看不到错误内容,只会产生50X错误。catch_workers_output=yes系统服务$cp/usr/src/php-5.6.30/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm$chmoda+x/etc/init.d/php-fpm#add进入init.d服务$chkconfig--addphp-fpm#设置服务自动运行$chkconfigphp-fpmonstart$ln-s/usr/local/php/bin/php/usr/bin/php$ln-s/usr/local/php/sbin/php-fpm/usr/bin/php-pfm#配置语法检测$php-pfm-t$servicephp-fpmstartMySQLuser#mysqlop行user$groupaddmysql$useraddmysql-gmysql-M$mkdir-p/usr/local/mysql$mkdir-p/usr/local/mysql/data$chown-Rmysql:mysql/usr/local/mysql$vi/etc/profile#profile追加以下内容PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATHexportPATH$source/etc/profile下载并解压源码下载源码包从MySQL官网,解压$tarzxvfmysql-5.6.33.tar.gz$cdmysql-5.6.33安装依赖$yum-yinstallncurses-develperlncurses-develbison-develcompileandinstallcmake\-DCMAKE_INSTALL_PREFIX=/usr/local/mysql\-DMYSQL_DATADIR=/usr/local/mysql/data\-DDEFAULT_CHARSET=utf8\-DDEFAULT_COLLATION=utf8_general_ci\-DMYSQL_TCP_PORT=3306\-DMYSQL_USER=mysql\-DWITH_MYISAM_STORAGE_ENGINE=1\-DWITH_INNOBASE_STORAGE_ENGINE=1\-DWITH_ARCHIVE_STORAGE_ENGINE=1\-DWITH_BLACKHOLE_STORAGE_ENGINE=1\-DWITH_MEMORY_STORAGE_ENGINE=1\-DDOWNLOAD_BOOST=1\-DWITH_BOOST=/usr/local/boost\$make&&makeinstall配置执行初始化:$cd/usr/local/mysql$./scripts/mysql_install_db--user=mysql--basedir=/usr/local/mysql--datadir=/usr/local/mysql/data修改配置文件:[mysqld]datadir=/usr/local/mysql/datasocket=/tmp/mysql.sockcharacter_set_server=utf8init_connect='SETNAMESutf8'#Restrictedtolocalaccessbind=127.0.0.1......[client]default-character-set=utf8系统服务$cp/usr/local/mysql/support-files/mysql.server/etc/init.d/mysqld$chkconfigmysqldon$servicemysqld启动权限$mysql-urootmysql>SETPASSWORD=PASSWORD('**********');mysql>GRANTALLPRIVILEGESON*.*TOroot@"127.0.0.1"WITHGRANTOPTION;mysql>FLUSHPRIVILEGES;Nginxpreparation#User$groupaddwww$useradd-gwwwwww-s/bin/false-M#Dependency$yum-yinstallpcre*opensll*下载解压官网下载源码包和解压缩$wgethttp://nginx.org/download/nginx-1.7.8.tar.gz$tarzxvfnginx-1.7.8.tar.gz$cdnginx-1.7.8编译安装$./configure--prefix=/usr/local/nginx--user=www--group=www--with-http_ssl_module--with-http_spdy_module--with-http_stub_status_module--with-pcre$make&&makeinstall配置nginx.conf文件配置:userwwwwww;worker_processes4;error_loglogs/error.lognotice;......http{......log_formatmain'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"'......server{listen80default;服务器名称_;返回403;}#各站配置文件includevhost/*.conf;......}各站配置文件:server{listen80;服务器名称本地主机;access_loglogs/host.access.logmain;位置/{root/home/www;indexindex.htmlindex.htm;}error_page500502503504/50x.html;location=/50x.html{rooHTML;}}SystemServices$vi/etc/init.d/nginx#添加以下内容#!/bin/bash#chkconfig:-8515#description:NginxisaWorldWideWebserver.#processname:nginxnginx=/usr/local/nginx/sbin/nginxconf=/usr/local/nginx/conf/nginx.confcase$1instart)echo-n"StartingNginx"$nginx-c$confecho"done";;stop)echo-n"StoppingNginx"killall-9nginxecho"done";;测试)$nginx-t-c$conf;;重新加载)echo-n“重新加载Nginx”psauxww|grepnginx|grep大师|awk'{print$2}'|xargskill-HUP回显“完成”;;restart)echo-n"RestartNginx"$0stopsleep1$0startecho"done";;显示)ps-aux|grepnginx;;*)echo-n"Usage:$0{start|restart|reload|stop|test|show}";;esac#保存并退出$cd/etc/init.d$chmod+xnginx$chkconfig--addnginx$chkconfig--level2345nginxon$chkconfignginxonstart#检测配置$servicenginxtest$servicenginxstartPhpstorm依赖Phpstorm,运行环境依赖JAVA,这里使用yum安装JAVA环境$yum-yinstalljava下载解压从phpstorm官网-Linux版下载最新的安装包。$tarzxvfPhpStorm-10.0.4.tar.gz$mv./PhpStorm-10.0.4/usr/local/phpstorminstallation注意:以下安装操作是从虚拟机终端进行的,并确保虚拟机正在运行在GUI模式下(init5)而不是通过Xshell端操作。在虚拟机终端中:$cd/usr/local/phpstorm/bin$./phpstorm.sh然后系统会弹出phpstorm安装界面,正常安装即可。通过IDEA注册码注册,获取正版注册码。本地代码实时同步开发环境我在Win下使用PhpStrom开发代码,开发环境是VM下的LNMP。那么如何将本地代码实时同步到开发环境进行开发调试呢?使用VM提供的文件共享可以很好的解决这个问题。关闭虚拟机后,在路径为EditVirtualMachineSettings>>Options>>SharedFolder的面板中配置共享文件夹的路径,即本地代码文件夹,选择AlwaysEnable选项启动虚拟机。共享文件夹默认挂载路径为/mnt/hgfs,如下:$cd/mnt/hgfs$ls#我的本地代码文件夹Code如果配置了共享文件夹路径,默认挂载路径没有共享文件夹,您可以尝试尝试以下解决方案:$cd/usr/src$gitclonehttps://github.com/rasa/vmware-tools-patches.git$cdvmware-tools-patches$./patched-open-vm-工具。之前sh启用Win10的Linux子系统的解决方案是使用虚拟机,后来是Docker。其实Win10已经支持Linux子系统了。这次我已经试过了。我安装的子系统是Ubuntu。一路走来并不顺利,所以把遇到的问题和相应的解决办法记录在这里。子系统用WIN自动启动Win后,需要手动Bash启动Ubuntu,极为不便。您可以使用vbs脚本启动Ubuntu并添加启动项。首先,创建一个名为ubuntu-up.vbs的vbs脚本,内容为:setws=wscript.createobject("wscript.shell")ws.run"C:\Windows\System32\bash.exe",0然后,在Type“运行”中的shell:startup,会打开“启动”列表,将ubuntu-up.vbs保存在这里,即可启动Ubuntu。子系统配置启动服务这里以SSH为例配置启动服务。Ubuntu子系统默认没有安装SSH,可以使用sudoapt-getinstallopenssh-server来完成安装。为了避免端口冲突,建议将Port配置为22以外的端口。首先尝试配置自启动服务:$sudochmod+x/etc/init.d/ssh$cd/etc/init.d/$sudoupdate-rc.dsshdefaults重启系统后发现sshd服务没有启动。找了半天终于用vbs脚本实现了。在子系统随Win自动启动的ubuntu-up.vbs脚本中添加如下内容:ws.run"C:\Windows\System32\bash.exe-c'sudo/usr/sbin/servicessh--full-restart'",0注意这里使用的是sudo命令,所以需要配置免密码使用,详见免密码使用sudo部分。重启后sshd服务会正常启动。更新[?]()使用Docker部署开发环境(2017-08-26)为Win10启用Linux子系统(2018-04-12)