ubuntu14.04 LNMP环境搭建笔记
近期学习小程序,需要搭建一个小型测试服务器,所以在阿里云开了一个1G内存的ubuntu服务器,准备用LNMP,在此记录下搭建过程。
1.启用密钥登录
阿里云默认使用用户名密码登录ubuntu系统。为了以后ssh连接方便,需要在阿里云控制台创建登录密钥对(控制台--》云服务器ECS--》菜单栏--》网络与安全),并下载秘钥文件到本地,进行无密码ssh登录,命令如下:ssh -i xxx.pem root@xxx.xxx.xxx.xxx。
2.安装MySQL数据库
sudo apt-get update
sudo apt-get install mysql-server mysql-client
//登录mysql 进行验证
mysql –u root –p"xxxxx"
修改MySQL配置文件/etc/mysql/my.cnf ,使MySQL支持中文:
//修改之前先备份
sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.back
sudo vi /etc/mysql/my.cnf
在配置文件的节点[client]下,加入
default-character-set = utf8
在[mysqld]下,加入
default-storage-engine = INNODB
character-set-server = utf8
collation-server = utf8_general_ci
然后重新加载文件,并重启MySQL服务:
sudo service mysql reload
sudo service mysql restart
登录MySQL,查看系统字符集,看到字符集是utf-8:
mysql -u root -p"xxx"
show variables like '%character%'
/* 查询结果如下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
*/
3.安装nginx
sudo apt-get install nginx
sudo service nginx start
安装完成后再浏览器中使用ip地址测试:http://xxx.xxx.xxx.xxx 。
4.安装php
sudo apt-get install php5-fpm
5.配置nginx
配置nginx.conf,将keepalive_timeout设置为2。(减小服务器压力?)
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back(备份)
sudo vi /etc/nginx/nginx.conf
配置/etc/nginx/sites-available/default:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.back
sudo vi /etc/nginx/sites-available/default
(1)http访问nginx配置:
root 后面的路径是存放php文件的路径,**如果需要修改为root目录下的路径,需要将root的目录的权限修改为551。
server_name 可以不修改
index index.html index.htm 这行后面添加 index.php
location ~ .php$ 是php配置的信息
server {
listen 80 default_server;
listen [::]:80 default_server;
root /root/xxxx;
index index.html index.htm index.php;
server_name _;
location / {
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root/xxxxxx;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
(2)https访问nginx配置:
配置https之前,需要先申请ssl证书。目前可以再阿里云申请免费的ssl证书,并下载证书文件,上传至自己的服务器。阿里云CA证书购买连接
https 配置与http配置基本相同,只是多了ssl证书的配置
ssl_certificate和ssl_certificate_key为证书路径信息
server {
listen 443;
server_name _;
root /root/xxxxxx;
index index.html index.htm index.php;
ssl on;
ssl_certificate /root/xxxxx/ssl/213969032760959.pem;
ssl_certificate_key /root/xxxx/ssl/213969032760959.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root/xxxxx;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
try_files $uri $uri/ =404;
}
}
6.配置php
在php.ini中设置cgi.fix_pathinfo=0,并反注释该行。
sudo cp /etc/php5/fpm/php.ini /etc/php5/fpm/php.ini.back
sudo vi /etc/php5/fpm/php.ini
//修改文件后重新加载
sudo service php5-fpm reload
sudo service php5-fpm restart
配置PHP支持MySQL:
sudo apt-get install php5-mysql php5-curl
7.验证
配置完成后,进行验证:http:// xxx.xxx.xxx.xxx/xxxx.php
vi /usr/share/nginx/html/info.php //修改为你配置的路径
/*文件内容
<?php
phpinfo();
?>
*/
vi /usr/share/nginx/html/mysqltest.php
/*文件内容
<?php
//连接mysql
$con = mysql_connect( "localhost", "root", "xxxx");
if ( ! $con ) {
echo 'Could not connect: ' . mysql_error();
die();
}
//创建一个数据库test
if( mysql_query( 'create database test' , $con ) ) {
echo 'Database created! ';
} else {
echo 'Database created error : ' . mysql_error();
}
mysql_close( $con );
?>
*/
8.一些小问题记录
lnmp问题记录