注意(本文主要介绍两种版本的lnmp环境配置)
Ubuntu16.04+nginx1.10+php7+Mysql5.7
1.更新
sudo apt update
sudo apt upgrade
2.安装nginx
sudo apt-get install nginx
3.安装mysql
sudo apt install mysql-server mysql-client
4.为了数据库安全,删除删除匿名用户和测试数据库
sudo mysql_secure_installation
接下来几个选项务必要选择正确
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] n
... skipping.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving... - Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
5.安装php7和扩展
sudo apt-get install php7.0-fpm php7.0-mbstring php7.0-xml php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl php-xmlrpc
6.配置PHP设置
sudo vim /etc/php/7.0/fpm/php.ini
找到cgi.fix_pathinfo,修改为:
cgi.fix_pathinfo=0
7.重启php
sudo service php7.0-fpm restart
8.Nginx中配置支持PHP
打开nginx配置文件
sudo vim /etc/nginx/sites-available/default
修改文件为:
server {
listen 80 default_server; #默认端口
listen [::]:80 default_server;
root /var/www/html; #站点目录
index index.php index.html index.htm index.nginx-debian.html; #添加index.php
server_name _ ; #可以添加自定义域名或者IP地址
location / {
# try_files $uri $uri/ =404; #如果用laravel需要注释掉这一行
try_files $uri $uri/ /index.php?$query_string; #添加url重定向
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
#server {
# listen 80;
# listen [::]:80 #这一部分模块,据说开启扩展后是nginx多站点配置
# server_name example.com;
# 不过我没有测试,暂且写在这里
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
修改完保存之后可以使用
sudo nginx -t
命令确认一下配置文件的正确性。
然后使用命令
sudo systemctl reload nginx
重启Nginx。
9.测试
新建文件/var/www/html/info.php,内容如下:
<?php
phpinfo();
?>
访问 http://localhost/info.php 查看结果
10.多站点配置
假如我们需要部署两个网站:
blog.com
test.com
设置两个新的文档目录
sudo mkdir -p /var/www/blog
sudo mkdir -p /var/www/test
11.为每个站点创建测试文件
创建文件/var/www/blog/info.php,内容为:
<?php
echo phpinfo();
?>
同样,创建文件/var/www/test//info.php,内容为:
<?php
echo phpinfo();
?>
12.为每个站点创建server block文件
如前所述,默认情况下Nginx已经配置了一个默认的server block,因此我们可以将默认的server block配置文件拷贝过来稍作修改:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/blog
打开文件:
sudo vim /etc/nginx/sites-available/blog
其内容如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _ ;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
修改后内容如下:
server {
listen 80; #去掉default_server,因为一台服务器只能有一个默认服务
listen [::]:80 ;
root /var/www/blog/; #如果要使用laravel,请把这个目录改为/var/www/blog/laravel/public/
index index.php index.html index.htm index.nginx-debian.html;
server_name _ www.blog.com blog.com ; #添加自己的网址
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
- 针对第二个站点test.com也做类似修改!
sudo cp /etc/nginx/sites-available/blog /etc/nginx/sites-available/test
sudo vim /etc/nginx/sites-available/test
13.激活两个站点的server block
sudo ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/test /etc/nginx/sites-enabled/
这样这些文件(链接)就位于激活的目录内了。到目前为止我们有3个激活了的server block了。服务器根据listen指令和server_name来确定该访问那个目录。
- blog: 响应来自blog.com以及www.blog.com的请求
- test: 响应来自test.com以及www.test.com的请求
- default: 响应没有匹配到上面两个规则的80端口的请求。
另外,还需要在nginx配置文件/etc/nginx/nginx.conf中设置下server_names_hash_bucket_size:
http {
. . .
server_names_hash_bucket_size 64;
. . .
}
然后检查下nginx配置文件的正确性:
sudo nginx -t
重启一下nginx使修改生效:
sudo systemctl restart nginx
13.本地测试
由于blog.com和test.com这两个域名并非我们真实拥有的域名,因此需要在本地机器修改下hosts来测试访问。
修改/etc/hosts文件:
127.0.0.1 localhost
. . .
XXX.XXX.XXX.XXX blog.com www.blog.com
XXX.XXX.XXX.XXX test.com www.test.com
前面的XXX.XXX.XXX.XXX即为服务器的外网IP。
本地可通过ifconfig查询本机IP,例如192.168.XX.XX
现在我们就可以直接访问blog.com/info.php和test.com/info.php来查看这两个站点了。
14.安装laravel
laravel安装方式有好几种,具体可参见官方文档
本机通过 Composer Create-Project 命令安装 Laravel
首先安装composer:
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
切换到需要安装的目录
cd /var/www/blog
创建项目
composer create-project laravel/laravel --prefer-dist
目录权限
sudo chown -R :www-data /var/www/blog/laravel
sudo chmod -R 775 /var/www/blog/laravel/bootstrap/
sudo chmod -R 775 /var/www/blog/laravel/storage/
好了,现在我们打开blog.com看一看是否成功!
选区_005.png- 如果发现页面空白,首先检查是否是目录权限问题,然后检查laravel目录下是否有vender目录,如果没有,执行:
composer install
这可能是因为你是通过git clone下来的laravel,所以缺少vender目录。
15.配置laravel环境配置
打开.env文件,如果没有复制一份.env.example文件为.env
APP_ENV=local
APP_KEY= #如果没有执行:php artisan key:generate
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databasename #数据库名
DB_USERNAME=root #用户名
DB_PASSWORD=password #密码
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
网友评论