Ubuntu16.04部署LNMP环境
- 2019 年 12 月 22 日
- 笔记
LNMP就是相对于LAMP环境的开发环境: LAMP = Linux + Apache + MySQL + PHP
LNMP = Linux + Nginx + MySQL + PHP
自己从网络上找了许多的教程,但是马虎的比较多,详细的还是没有几个,并且有许多的Bug,现在将自己踩的坑做出来的教程呈上。
LNMP就是采用nginx服务器来进行作为服务器应用运行环境,具体怎么设置,请看下文:
升级系统
sudo apt update
安装Nginx
sudo apt install nginx
默认web路径在/var/www/html
, 访问本地http://loaclhost/
出现Welcome to Nginx!
,即是安装成功!
安装Mysql
sudo apt install mysql-server mysql-client
安装过程中需设置 mysql root
密码 mysql_secure_installation #加强安全性
,可选
安装PHP
安装环境检测
# 如果之前有其他版本PHP,先卸载 sudo apt-get autoremove php5php5-fpmphp7
配置安装源并且安装
sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository ppa:ondrej/php && sudo apt-get update # 安装PHP7.2 sudo apt-get -y install php7.2
安装PHP及常用扩展
sudo apt-get install php7.2-fpm php7.2-mysql php7.2-curl php7.2-json php7.2-mbstring php7.2-xml php7.2-intl -y
安装其他扩展(按需安装)
sudo apt-get install php7.2-gd sudo apt-get install php7.2-soap sudo apt-get install php7.2-gmp sudo apt-get install php7.2-odbc sudo apt-get install php7.2-pspell sudo apt-get install php7.2-bcmath sudo apt-get install php7.2-enchant sudo apt-get install php7.2-imap sudo apt-get install php7.2-ldap sudo apt-get install php7.2-opcache sudo apt-get install php7.2-readline sudo apt-get install php7.2-sqlite3 sudo apt-get install php7.2-xmlrpc sudo apt-get install php7.2-bz2 sudo apt-get install php7.2-interbase sudo apt-get install php7.2-pgsql sudo apt-get install php7.2-recode sudo apt-get install php7.2-sybase sudo apt-get install php7.2-xsl sudo apt-get install php7.2-cgi sudo apt-get install php7.2-dba sudo apt-get install php7.2-phpdbg sudo apt-get install php7.2-snmp sudo apt-get install php7.2-tidy sudo apt-get install php7.2-zip
Nginx配置PHP环境
默认配置文件位置:/etc/nginx/sites-availble
文件夹下:
新建一个文件名为自己域名的文件,例如 “debuginn.cn.conf
”,文件内容为:
server { listen 443; server_name www.debuginn.cn; root /var/www/html/www.debuginn.cn; index index.html index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ .php$ { include snippets/fastcgi-php.conf; # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.2-fpm.sock; } } server { listen 80; server_name www.debuginn.cn; return 301 http://$server_name$request_uri; }
升级HTTPS服务文件配置(非必须)
server { listen 443; server_name www.debuginn.cn; ssl on; root /var/www/html/www.debuginn.cn; index index.html index.php; ssl_certificate cert/1826431_www.debuginn.cn.pem; ssl_certificate_key cert/1826431_www.debuginn.cn.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { try_files $uri $uri/ /index.php?$args; } location ~ .php$ { include snippets/fastcgi-php.conf; # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.2-fpm.sock; } } server { listen 80; server_name www.debuginn.cn; return 301 https://$server_name$request_uri; }
添加重定向 (非必须)
将http
重定向为https
:
server { listen 80; server_name www.debuginn.cn; return 301 https://$server_name$request_uri; }
return 301 https://$server_name$request_uri;
连接配置信息
ln -s /etc/nginx/sites-available /etc/nginx/sites-enabled
添加host文件
hosts文件位置在 /etc/hosts
添加一行记录,如下:
127.0.0.1 www.debuginn.cn
OK 配置完毕,希望对你有用哦!