安装PHP 7.1
apt-get update
apt-get install -y language-pack-en-base
,安装 Php7.1 之前,要先安装language-pack-en-base
这个包这个包是为了解决系统不同语言之间可能发生的冲突,安装之后可以减少许多因语言编码带来的问题。其中
-y
参数表明直接安装,无需确认。locale-gen en_US.UTF-8
,设定语言编码为UTF-8
apt-get install git vim
,安装Git、Vim下面采用ppa方式安装php7.1:
apt-get install software-properties-common
,software-properties-common
是add-apt-repository
所依赖的包LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
,添加php7的ppa,注意LC_ALL=en_US.UTF-8
参数告诉我们系统语言为UTF-8,如果没有,可能会出现错误,如阿里云的服务器。apt-get update
,更新安装包,把刚才添加的包拉取下来apt-cache search php7.1
,搜索php7.1开头的包检验是否安装成功,输出如下:root@demo:~# apt-cache search php7.1 php-yaml - YAML-1.1 parser and emitter for PHP php-apcu - APC User Cache for PHP php-ssh2 - Bindings for the libssh2 library php-igbinary - igbinary PHP serializer php-mailparse - Email message manipulation for PHP php-libsodium - PHP wrapper for the Sodium cryptographic library php-propro - propro module for PHP php-raphf - raphf module for PHP ....................省略...........................
apt-get -y install php7.1
,安装phpphp -v
,查看是否安装成功,成功则输出如下:PHP 7.1.0beta2 (cli) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.1.0beta2, Copyright (c) 1999-2016, by Zend Technologies
apt-get -y install php7.1-mysql
,安装php7.1-mysql,Php7.1 与 mysql 的通信模块apt-get install php7.1-fpm
,安装 fpm,这是Nginx 用来解析php文件的apt-get install php7.1-curl php7.1-xml php7.1-mcrypt php7.1-json php7.1-gd php7.1-mbstring php7.1-zip
,安装其他必备模块// 查看已安装模块 php -m
安装MySQL
apt-get update
apt-get install mysql-server mysql-client
,安装MySQLmysql -V
,查看版本,输出如下:root@demo:~# mysql -V mysql Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using EditLine wrapper
启动与停止
service mysql start service mysql stop
是否在运行
netstat -tap | grep mysql
安装Nginx
apt update
-
apt-get -y install nginx
,安装Nginx - 访问IP可查看到Nginx引导页
配置PHP
-
vim /etc/php/7.1/fpm/php.ini
,输入/fix_pathinfo
搜索,将cgi.fix_pathinfo=1
改为`cgi.fix_pathinfo=0“
-
编辑fpm的配置文件
-
vim /etc/php/7.1/fpm/pool.d/www.conf
,找到listen = /run/php/php7.1-fpm.sock
修改为listen = /var/run/php/php7.1-fpm.sock
。当然,你也可以不修改,但必须前后一致,后面会用到这个配置
-
配置Nginx
vim /etc/nginx/sites-available/default
,主要是配置server这一部分,最终配置如下:server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/[project-name]/public; index index.php index.html index.htm; # Make site accessible from http://localhost/ server_name xxx.xx; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ /index.php?$query_string; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
解释:
-
root
:是你的项目的public目录,也就是网站的入口 -
index
:添加了,index.php
,告诉Nginx先解析index.php
文件 -
server_name
:你的域名,没有的话填写localhost
-
location / try_files
修改为了try_files $uri $uri/ /index.php?$query_string;
-
location ~ \.php$
部分告诉Nginx怎么解析Php,原封不动复制即可,但注意:fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;的目录要和fpm的配置文件中的listen一致。
-
Vue工程部署到Ngnix
-
npm run build
,编译Vue工程,工程根目录下生成dist
目录 - 查看
nginx
配置文件(/etc/nginx/sites-available/default
),查看root
路径(如:root /var/www/vitahome/public;
),将dist
目录下内容复制到该路径下 - 浏览器中访问IP或域名即可
Laravel工程部署到Nginx
备注:当前服务器Nginx上已部署了一个Vue工程
创建Laravel项目
composer
安装cd ~ curl -sS https://getcomposer.org/installer | php
设置
composer
全局使用mv composer.phar /usr/local/bin/composer
创建
Laravel
工程:vitahomebackend
,在/var/www
目录下直接执行:composer create-project laravel/laravel vitahomebackend
/var/www/
是属于root
用户的,而访问网站的用户则需要正确的权限和访问限制:chmod -R :www-data /var/www/vitahomebackend
根据Laravel的官方文档,
/var/www/vitahomebackend/storage
目录需要给网站的用户写
权限chmod -R 775 /var/www/vitahomebackend/storage
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/vitahomebackend
,复制default配置文件到其目录下,换个名字,用于要部署的Lavavel工程vim /etc/nginx/sites-available/vitahomebackend
,修改Lavarel工程配置文件(:set nu
打开行号),修改后如下:## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # http://wiki.nginx.org/Pitfalls # http://wiki.nginx.org/QuickStart # http://wiki.nginx.org/Configuration # # Generally, you will want to move this file somewhere, and start with a clean # file but keep this around for reference. Or just disable in sites-enabled. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { // 注释掉了原来的配置,下面那条是自己手写的80端口 # listen 80 default_server; # listen [::]:80 default_server; listen 80; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; // 配置本地的laravel工程目录 root /var/www/vitahomebackend/public; # Add index.php to the list if you are using PHP // 添加了一个 index.php ,原来是没有的 index index.php index.html index.htm index.nginx-debian.html; // 域名或IP,注意:域名需要在阿里云后台增加域名解析 server_name xxx.xxxx.xxx; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404; // 将下面的指令放到站点配置文件中就可以实现美化链接的功能 try_files $uri $uri/ /index.php?$query_string; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # Virtual Host configuration for example.com # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } # }
注意:Lavarel工程的配置文件内容与Vue工程的不一样,必须要修改,否则在添加软连接后启动Nginx会报错:
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
Nginx启动与停止
service nginx stop service nginx start
配置站点,添加软连接
cd /etc/nginx/sites-enabled ln -s /etc/nginx/sites-available/vitahomebackend ./
执行后,
/etc/nginx/sites-enabled
目录下会生成一个文件vitahomebackend
重启Nginx,成功后即可用域名访问对应的工程页面
php artisan serve
运行报错
错误信息:
PHP Warning: require(/var/www/vitahomebackend/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/vitahomebackend/artisan on line 18 PHP Fatal error: require(): Failed opening required '/var/www/vitahomebackend/vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/vitahomebackend/artisan on line 18
错误原因:
网站根目录下缺少vendor这个存放laraver依赖包的目录
解决方法:
- 没有安装过的情况下,使用
composer install
进行安装 - 以前安装过的话使用:
composer update
- 没有安装过的情况下,使用