LNMP环境搭建
工作流
- 用户访问域名网站,DNS解析成对应的IP
- IP:80 端口,访问到WebServer Nginx,如果是静态目录,Nginx直接将请求返回给用户端
- 如果是动态PHP文件,Nginx会将PHP文件转发给PHP-FPH PHP核心解析器,同时,解析器对PHP代码进行解析,如果涉及到数据库操作类的动作,会和数据库作交互。 最后将解析后的PHP代码返回给Nginx,Nginx最后将结果再呈现给用户。
环境部署
安装Nginx
#1)安装nginx
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
systemctl start nginx
systemctl enable nginx
systemctl status nginx.service
安装MySql
#2)安装MySql
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum makecache
yum -y install mysql-community-server
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld.service
# 执行以下命令,获取安装MySQL时自动设置的root用户密码。
grep 'temporary password' /var/log/mysqld.log
#回显如下类似信息。
#加固MySql并且设置安全策略
mysql_secure_installation
[ host201:~ ]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : n
... skipping.
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? (Press y|Y for Yes, any other key for No) : n
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
#数据库登录
mysql -uroot -proot123#
mysq -h 192.168.3.50 -P 3306 -uroot -proot
安装MySql
#3 安装PHP
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php70w-tidy php70w-common php70w-devel php70w-pdo php70w-mysql php70w-gd php70w-ldap php70w-mbstring php70w-mcrypt php70w-fpm
php -v
systemctl start php-fpm
systemctl enable php-fpm
修改nginx 配置,让webserver nginx 能识别index.php文件,从而将php动态代码转发给PHP-FPM PHP解析器能
# 4 修改nginx 配置,让webserver nginx 能识别index.php文件,从而将php动态代码转发给PHP-FPM PHP解析器能
vim /etc/nginx/nginx.conf
[ host201:/etc/nginx ]# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[ host201:/etc/nginx/conf.d ]# cat nginx.conf | grep -Ev '^$|#'
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
编写PHP代码测试验证
FAQ
Nginx配置文件中,/etc/nginx/nginx.conf中有且只有一个http配置项目,一个http可以对应多个Server,server 文件可以配置在该文件中,也可以配置在/etc/nginx/conf.d/*.conf下面,文件以.conf结尾。