lnmp 环境搭建好后 tp项目 403,TP框架配置

阅读:444 2019-03-19 14:43:26 来源:新网

lnmp项目搭建好后--把thinkphp放进去后报错|403

是因为nginx不支持tp的访问模式|需要额外配置

tp框架配置中默认url_model=1,而nginx默认是不支持pathinfo的。如果我们只想跑起来tp框架,很简单,只需到更改tp配置,设置url_model=3(兼容模式)。但是如果要让nginx支持thinkphppathinfo需要做如下配置:

1、设置thinkphpurl模式url_model=1;

2、修改nginx配置文件(红色部分更改称相应的内容)

server{listen80;server_namewww.你的域名.com;indexindex.php;root/users/just/git/myblog;location/{if(!-e$request_filename){rewrite^/(.*)$/index.php/$1last;break;}}location~.php{fastcgi_pass127.0.0.1:9000;fastcgi_indexindex.php;includefastcgi_params;set$real_script_name$fastcgi_script_name;if($fastcgi_script_name~"^(.+?.php)(/.+)$"){set$real_script_name$1;set$path_info$2;}fastcgi_paramscript_filename$document_root$real_script_name;fastcgi_paramscript_name$real_script_name;fastcgi_parampath_info$path_info;}}

3、保存nginx配置并重启

推荐使用yum安装nginx:

sudoyuminstallnginx

输入yes后开始安装。安装完毕后,nginx的配置文件在/etc/nginx目录下。使用以下命令启动nginx:

sudoservicenginxstart

可以通过浏览器访问http://<外网ip地址>来确定nginx是否已经启动。

最后我们通过chkconfig命令将nginx设置为开机启动:

sudochkconfignginxon

我们可以使用yum直接安装mysql:

sudoyuminstallmysql-server

安装完成之后,执行以下命令重启mysql服务:

sudoservicemysqldrestart

重启过程中会提示如何设置mysql的root密码,我们按照提示进行操作:

sudo/usr/bin/mysql_secure_installation

这个脚本会经过一些列的交互问答来进行mysql的安全设置。

首先提示输入当前的root密码:

entercurrentpasswordforroot(enterfornone):

初始root密码为空,我们直接敲回车进行下一步。

setrootpassword?[y/n]

设置root密码,默认选项为yes,我们直接回车,提示输入密码,在这里设置您的mysql的root账户密码。

removeanonymoususers?[y/n]

是否移除匿名用户,默认选项为yes,建议按默认设置,回车继续。

disallowrootloginremotely?[y/n]

是否禁止root用户远程登录?如果您只在本机内访问mysql,建议按默认设置,回车继续。如果您还有其他云主机需要使用root账号访问该数据库,则需要选择n。

removetestdatabaseandaccesstoit?[y/n]

是否删除测试用的数据库和权限?建议按照默认设置,回车继续。

reloadprivilegetablesnow?[y/n]

是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车。

完成后你会看到success!的提示,mysql的安全设置已经完成。我们可以使用以下命令登录mysql:

mysql-uroot-p

按提示输入root密码,就会进入mysql的交互界面,说明已经安装成功。

mysql安装完成后默认监听外网的3306端口,有一定的安全隐患,我们通过编辑mysql的配置文件,将mysql服务修改为监听内网的3306端口,这样就不会被外界探测到,编辑/etc/my.cnf文件,在[mysqld]中增加一行:

bind-address=10.128.129.21

需要将上面的10.128.129.21替换成您的云主机的内网地址。内网地址可以从mos控制台云主机列表中找到。然后重启mysql服务使配置生效:

sudoservicemysqldrestart

最后我们使用chkconfig命令将mysql设置为开机启动。

sudochkconfigmysqldon

然后执行以下命令使我们的配置生效:

sudoservicenginxreload

以上我们配置了nginx的8000端口用来测试,如果您在美团云控制台创建机器时选择了绑定防火墙,需要检查该防火墙是否允许8000端口,如果不允许的话,您可以在防火墙设置中新增防火墙,并关联到该主机。

我们在/usr/share/php目录下新建一个名为phpinfo.php的文件用来展示phpinfo信息,文件内容为:

我们从浏览器打开http://<外网ip地址>:8000/phpinfo.php,您就能看到phpinfo信息了,说明我们php环境已经部署成功:

验证php安装成功后,需要将此phpinfo.php文件删除,线上环境尽量不要暴漏使用的软件版本及路径信息,以防被入侵者利用

cd/usr/local/src/wgethttp://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz解压

[root@localhostsrc]#tarzxvf/usr/local/src/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz把解压完的数据移动到/usr/local/mysql

[root@localhostsrc]#mvmysql-5.1.40-linux-i686-icc-glibc23/usr/local/mysql建立mysql用户

[root@localhostsrc]#useradd-s/sbin/nologinmysql初始化数据库

[root@localhostsrc]#cd/usr/local/mysql[root@localhostmysql]#mkdir-p/data/mysql;chown-rmysql:mysql/data/mysql[root@localhostmysql]#./scripts/mysql_install_db--user=mysql--datadir=/data/mysql

--user定义数据库的所属主,--datadir定义数据库安装到哪里,建议放到大空间的分区上,这个目录需要自行创建。这一步骤很关键,如果你看到两个“ok”说明执行正确,否则请仔细查看错误信息,如果你实在解决不了,

[root@localhostmysql]#cpsupport-files/my-large.cnf/etc/my.cnf拷贝启动脚本文件并修改其属性

[root@localhostmysql]#cpsupport-files/mysql.server/etc/init.d/mysqld[root@localhostmysql]#chmod755/etc/init.d/mysqld修改启动脚本

[root@localhostmysql]#vim/etc/init.d/mysqld

需要修改的地方有“datadir=/data/mysql”(前面初始化数据库时定义的目录)

[root@localhostmysql]#chkconfig--addmysqld[root@localhostmysql]#chkconfigmysqldon[root@localhostmysql]#servicemysqldstart

如果启动不了,请到/data/mysql/下查看错误日志,这个日志通常是主机名.err.检查mysql是否启动的命令为:

[root@localhostmysql]#psaux|grepmysqld安装php

nginx中的php是以fastcgi的方式结合nginx的,可以理解为nginx代理了php的fastcgi,而apache是把php作为自己的模块来调用的。

[rot@localhosthttpd-2.2.24]#cd/usr/local/src[root@localhostsrc]#wgethttp://am1.php.net/distributions/php-5.3.27.tar.gz解压php

[root@localhostsrc]#tarzxfphp-5.3.27.tar.gz创建相关账户

[root@localhostsrc]#useradd-s/sbin/nologinphp-fpm配置编译参数

[root@localhostsrc]#cdphp-5.3.27[root@localhostphp-5.3.27]#./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--enable-fpm--with-fpm-user=php-fpm--with-fpm-group=php-fpm--with-mysql=/usr/local/mysql--with-mysql-sock=/tmp/mysql.sock--with-libxml-dir--with-gd--with-jpeg-dir--with-png-dir--with-freetype-dir--with-iconv-dir--with-zlib-dir--with-mcrypt--enable-soap--enable-gd-native-ttf--enable-ftp--enable-mbstring--enable-exif--disable-ipv6--with-pear--with-curl--with-openssl

该过程中,如果出现如下错误,

错误信息:

configure:error:pleasereinstallthelibcurldistribution-easy.hshouldbein/include/curl/

解决办法:

yuminstall-ylibcurl-devel编译php

[root@localhostphp-5.3.27]#make

在这一步,你通常会遇到一些错误

/usr/bin/ld:cannotfind-lltdlcollect2:ldreturned1exitstatusmake:***[sapi/fpm/php-fpm]错误1

解决

yuminstall-ylibtool-ltdl-devel安装php

[root@localhostphp-5.3.27]#makeinstall

以上每一个步骤,如果没有完全执行正确,那么下一步是无法进行的,是否还记得判断执行是否正确的方法?使用echo$?看结果是否为“0”,如果不是,就是没有执行正确。

cpphp.ini-production/usr/local/php/etc/php.inivim/usr/local/php/etc/php-fpm.conf

把如下内容写入该文件:

[global]pid=/usr/local/php/var/run/php-fpm.piderror_log=/usr/local/php/var/log/php-fpm.log[www]listen=/tmp/php-fcgi.sockuser=php-fpmgroup=php-fpmpm=dynamicpm.max_children=50pm.start_servers=20pm.min_spare_servers=5pm.max_spare_servers=35pm.max_requests=500rlimit_files=1024

保存配置文件后,检验配置是否正确的方法为:

/usr/local/php/sbin/php-fpm-t

如果出现诸如“testissuccessful”字样,说明配置没有问题。

cp/usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpmchmod755/etc/init.d/php-fpmservicephp-fpmstart

如果想让它开机启动,执行:

chkconfigphp-fpmon

检测是否启动:

psaux|grepphp-fpm

看看是不是有很多个进程(大概20多个)。

nginx官方网站(http://nginx.org),从官方网站可以看到nginx更新速度很快,这也反映了一个事实,目前使用nginx跑网站的公司或者个人越来越多。当前最新版本为1.5,但是阿铭不建议你安装这么新的,因为它还太新,难免会有一些bug或者漏洞,所以阿铭建议你安装1.4版本的nginx.

(近期nginx报出一个安全漏洞,影响版本很广cve-2013-4547,所以之前的老版本都需要升级一下,1.4.4,1.5.7以及往后版本没有问题)

cd/usr/local/src/wgethttp://nginx.org/download/nginx-1.4.4.tar.gz解压nginx

tarzxvfnginx-1.4.4.tar.gz配置编译参数

cdnginx-1.4.4./configure--prefix=/usr/local/nginx--with-http_realip_module--with-http_sub_module--with-http_gzip_static_module--with-http_stub_status_module--with-pcre编译nginx

make安装nginx

makeinstall

因为nginx比较小,所以很快就会安装完,而且也不会出什么错误,如果出错了,到阿铭论坛(http://www.aminglinux.com/bbs/forum-40-1.html)发帖求助阿铭吧。

vim/etc/init.d/nginx

写入如下内容:

#!/bin/bash#chkconfig:-3021#description:httpservice.#sourcefunctionlibrary./etc/init.d/functions#nginxsettingsnginx_sbin="/usr/local/nginx/sbin/nginx"nginx_conf="/usr/local/nginx/conf/nginx.conf"nginx_pid="/usr/local/nginx/logs/nginx.pid"retval=0prog="nginx"start(){echo-n$"starting$prog:"mkdir-p/dev/shm/nginx_tempdaemon$nginx_sbin-c$nginx_confretval=$?echoreturn$retval}stop(){echo-n$"stopping$prog:"killproc-p$nginx_pid$nginx_sbin-termrm-rf/dev/shm/nginx_tempretval=$?echoreturn$retval}reload(){echo-n$"reloading$prog:"killproc-p$nginx_pid$nginx_sbin-hupretval=$?echoreturn$retval}restart(){stopstart}configtest(){$nginx_sbin-c$nginx_conf-treturn0}case"$1"instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo$"usage:$0{start|stop|reload|restart|configtest}"retval=1esacexit$retval

保存后,更改权限:

chmod755/etc/init.d/nginxchkconfig--addnginx

如果想开机启动,请执行:

chkconfignginxon更改nginx配置

首先把原来的配置文件清空:

>/usr/local/nginx/conf/nginx.conf

“>”为重定向的意思,单独用它,可以把一个文本文档快速清空。

vim/usr/local/nginx/conf/nginx.conf

写入如下内容:

usernobodynobody;worker_processes2;error_log/usr/local/nginx/logs/nginx_error.logcrit;pid/usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile51200;events{useepoll;worker_connections6000;}http{includemime.types;default_typeapplication/octet-stream;server_names_hash_bucket_size3526;server_names_hash_max_size4096;log_formatcombined_realip'$remote_addr$http_x_forwarded_for[$time_local]''$host"$request_uri"$status''"$http_referer""$http_user_agent"';sendfileon;tcp_nopushon;keepalive_timeout30;client_header_timeout3m;client_body_timeout3m;send_timeout3m;connection_pool_size256;client_header_buffer_size1k;large_client_header_buffers84k;request_pool_size4k;output_buffers432k;postpone_output1460;client_max_body_size10m;client_body_buffer_size256k;client_body_temp_path/usr/local/nginx/client_body_temp;proxy_temp_path/usr/local/nginx/proxy_temp;fastcgi_temp_path/usr/local/nginx/fastcgi_temp;fastcgi_intercept_errorson;tcp_nodelayon;gzipon;gzip_min_length1k;gzip_buffers48k;gzip_comp_level5;gzip_http_version1.1;gzip_typestext/plainapplication/x-javascripttext/csstext/htmapplication/xml;server{listen80;server_namelocalhost;indexindex.htmlindex.htmindex.php;root/usr/local/nginx/html;location~.php${includefastcgi_params;fastcgi_passunix:/tmp/php-fcgi.sock;fastcgi_indexindex.php;fastcgi_paramscript_filename/usr/local/nginx/html$fastcgi_script_name;}}}

保存配置后,先检验一下配置文件是否有错误存在:

/usr/local/nginx/sbin/nginx-t

如果显示内容如下,则配置正确,否则需要根据错误提示修改配置文件:

nginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisoknginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful

启动nginx:

servicenginxstart

如果不能启动,请查看“/usr/local/nginx/logs/error.log”文件,检查nginx是否启动:

psaux|grepnginx

看是否有进程。

创建测试文件:

vim/usr/local/nginx/html/2.php

内容如下:

测试:

[root@localhostnginx]#curllocalhost/2.php测试php是否解析[root@localhostnginx]#

这样,才说明php解析正确。

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服