This article mainly introduces in detail how to deploy Laravel on the cloud server, which has certain reference value. Interested friends can refer to it
It has been a while since I learned PHP and Laravel, but All the code ran on the local virtual host, so I went to Tencent Cloud to apply for a free cloud host for one month, and wanted to deploy the project to the cloud server.
I have to say that there are a lot of pitfalls here, which makes me, a novice who comes into contact with the server for the first time, confused. After configuring the server, deploying a Laravel project is even more difficult, so I wanted to record the process of deploying the Laravel project.
PS: Linux is really a system that feels better the more you use it. You should install Linux on your desktop computer to type code when you go home.
Environment Introduction
In terms of the choice of operating system, I chose the Linux ubuntu16.04 system and used the LNMP environment, that is, Linux + Nginx + Mysql + PHP environment.
Delete Apache
sudo service apache2 stop update-rc.d -f apache2 remove sudo apt-get remove apache2
Use these three commands to delete Apaceh first and then update the package list
sudo apt-get update
1. Install Nginx
sudo apt-get install nginx
After installing Nginx, you need to restart nginx
sudo service nginx start
After execution, enter the public IP assigned to you by the cloud server in the browser, and you can see the welcome to nginx The interface is
2. During the installation of Mysql
sudo apt-get install mysql-server mysql-client
, you will be prompted to set the Mysql password, just like the usual password settings, enter it once and confirm it once. After the password is confirmed, the installation will basically take a while. Try
mysql -u root -p
If the login is successful, Mysql is installed correctly.
3. Install PHP
sudo apt-get install php5-fpm php5-cli php5-mcrypt
Only through php5-fpm, PHP can run normally under Nginx, so install it.
As for php5-mcrypt, some PHP frameworks will depend on this, such as Laravel, so it is also installed.
Off topic, I installed php7 myself during deployment of php5 here. If you want to try it, you can also try it.
4. Configure PHP
sudo vim /etc/php5/fpm/php.ini
Open the PHP configuration file, find the cgi.fix_pathinfo option, remove the comment semicolon; in front of it, and then Set its value to 0, as follows
cgi.fix_pathinfo=0
5. Enable php5-mcrypt:
sudo php5enmod mcrypt
6.Restart php5-fpm:
sudo service php5-fpm restart
After setting up the LEMP environment, you must first clarify two important directories
Nginx’s default root folder
##/usr/ share/nginx/html
The directory where Nginx’s server configuration file is located
/etc/nginx/sites-available/
The following is a step-by-step deployment of Laravel on the cloud server
1. Create the root directory of the website
sudo mkdir -p /var/www
2. Configure the nginx server
sudo vim /etc/nginx/sites-available/default
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; } }
root modification
root /var/www/laravel/public;
index modification
index index.php index.html index.htm;
server_name modification
server_name server_domain_or_IP;
location modification
location / { try_files $uri $uri/ /index.php?$query_string; }
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/laravel/public; index index.php index.html index.htm; server_name server_domain_or_IP; location / { try_files $uri $uri/ /index.php?$query_string; } }
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/laravel/public; index index.php index.html index.htm; server_name server_domain_or_IP; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
location ~ \.php$ was added by yourself:
sudo service nginx restart
3. Create a Laravel project
After configuring nginx, how toget the Laravel project code? There are several methods:
(1). Direct composer installation Install directly through composer. You can executecd ~ curl -sS getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo composer create-project laravel/laravel laravel
(2). Upload the code directly
Use the following command to uploadscp -r laravel root@your_IP:
sudo mv laravel/ /var/www
(3). Use Git and Coding platform
I personally prefer to use git to upload code, which can easily update the code and roll back. Once the version When bugs are updated, I can use Git's powerful version management capabilities to fix them. The process is roughly like this: Local code---->Github---->Cloud server既然要使用git,那么先在云服务器上安装git:
sudo apt-get install git
安装完成就可以使用git了,然后在Github上创建一个私有项目laravel,里面包含所有该Laravel项目所需代码。
一旦本地代码都推送到Coding,然后在/var/www目录下直接使用
git clone your-project-git-link
your-project-git-link替换为你Github上的laravel项目地址
5.BINGO
在浏览器输入:server_domain_or_IP
至此,你可以在服务器上随意地用Laravel了,keep coding!
The above is the detailed content of Example tutorial of deploying Laravel on cloud server. For more information, please follow other related articles on the PHP Chinese website!