注意(本文主要介紹兩種版本的lnmp環(huán)境配置)
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.為了數(shù)據(jù)庫安全鳞芙,刪除刪除匿名用戶和測試數(shù)據(jù)庫
sudo mysql_secure_installation
接下來幾個(gè)選項(xiàng)務(wù)必要選擇正確
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和擴(kuò)展
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設(shè)置
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; #默認(rèn)端口
listen [::]:80 default_server;
root /var/www/html; #站點(diǎn)目錄
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 #這一部分模塊陶舞,據(jù)說開啟擴(kuò)展后是nginx多站點(diǎn)配置
# server_name example.com;
# 不過我沒有測試鸥鹉,暫且寫在這里
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
修改完保存之后可以使用
sudo nginx -t
命令確認(rèn)一下配置文件的正確性。
然后使用命令
sudo systemctl reload nginx
重啟Nginx豌拙。
9.測試
新建文件/var/www/html/info.php,內(nèi)容如下:
<?php
phpinfo();
?>
訪問 http://localhost/info.php 查看結(jié)果
10.多站點(diǎn)配置
假如我們需要部署兩個(gè)網(wǎng)站:
blog.com
test.com
設(shè)置兩個(gè)新的文檔目錄
sudo mkdir -p /var/www/blog
sudo mkdir -p /var/www/test
11.為每個(gè)站點(diǎn)創(chuàng)建測試文件
創(chuàng)建文件/var/www/blog/info.php,內(nèi)容為:
<?php
echo phpinfo();
?>
同樣,創(chuàng)建文件/var/www/test//info.php,內(nèi)容為:
<?php
echo phpinfo();
?>
12.為每個(gè)站點(diǎn)創(chuàng)建server block文件
如前所述,默認(rèn)情況下Nginx已經(jīng)配置了一個(gè)默認(rèn)的server block,因此我們可以將默認(rèn)的server block配置文件拷貝過來稍作修改:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/blog
打開文件:
sudo vim /etc/nginx/sites-available/blog
其內(nèi)容如下:
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;
}
}
修改后內(nèi)容如下:
server {
listen 80; #去掉default_server题暖,因?yàn)橐慌_(tái)服務(wù)器只能有一個(gè)默認(rèn)服務(wù)
listen [::]:80 ;
root /var/www/blog/; #如果要使用laravel按傅,請(qǐng)把這個(gè)目錄改為/var/www/blog/laravel/public/
index index.php index.html index.htm index.nginx-debian.html;
server_name _ www.blog.com blog.com ; #添加自己的網(wǎng)址
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;
}
}
- 針對(duì)第二個(gè)站點(diǎn)test.com也做類似修改!
sudo cp /etc/nginx/sites-available/blog /etc/nginx/sites-available/test
sudo vim /etc/nginx/sites-available/test
13.激活兩個(gè)站點(diǎn)的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/
這樣這些文件(鏈接)就位于激活的目錄內(nèi)了。到目前為止我們有3個(gè)激活了的server block了胧卤。服務(wù)器根據(jù)listen指令和server_name來確定該訪問那個(gè)目錄逞敷。
- blog: 響應(yīng)來自blog.com以及www.blog.com的請(qǐng)求
- test: 響應(yīng)來自test.com以及www.test.com的請(qǐng)求
- default: 響應(yīng)沒有匹配到上面兩個(gè)規(guī)則的80端口的請(qǐng)求。
另外,還需要在nginx配置文件/etc/nginx/nginx.conf中設(shè)置下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這兩個(gè)域名并非我們真實(shí)擁有的域名,因此需要在本地機(jī)器修改下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即為服務(wù)器的外網(wǎng)IP推捐。
本地可通過ifconfig查詢本機(jī)IP,例如192.168.XX.XX
現(xiàn)在我們就可以直接訪問blog.com/info.php和test.com/info.php來查看這兩個(gè)站點(diǎn)了。
14.安裝laravel
laravel安裝方式有好幾種侧啼,具體可參見官方文檔
本機(jī)通過 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
創(chuàng)建項(xiàng)目
composer create-project laravel/laravel --prefer-dist
目錄權(quán)限
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/
好了牛柒,現(xiàn)在我們打開blog.com看一看是否成功!
- 如果發(fā)現(xiàn)頁面空白痊乾,首先檢查是否是目錄權(quán)限問題皮壁,然后檢查laravel目錄下是否有vender目錄,如果沒有哪审,執(zhí)行:
composer install
這可能是因?yàn)槟闶峭ㄟ^git clone下來的laravel,所以缺少vender目錄蛾魄。
15.配置laravel環(huán)境配置
打開.env文件,如果沒有復(fù)制一份.env.example文件為.env
APP_ENV=local
APP_KEY= #如果沒有執(zhí)行: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 #數(shù)據(jù)庫名
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