熟悉LNMP使用场景
安装nginx
安装mysql或mariadb扩展
安装php7.X
配置nginx虚拟主机【基于端口、ip地址、域名】
实现反向代理和负载均衡
发布应用程序进行测试
**
Systemctl stop firewalld
Sentenforce 0
yum -y install nginx
nginx -v
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
yum -y install mysql-community-server
mysql -V
systemctl start mysqld
systemctl enable mysqld
systemctl daemon-reload
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
php -v
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vim /etc/nginx/nginx.conf
location / {
index index.php index.html index.htm;
}
location ~ .php$ {
root /usr/share/nginx/html; #将/usr/share/nginx/html替换为您的网站根目录,本教程使用/usr/share/nginx/html作为网站根目录。
fastcgi_pass 127.0.0.1:9000; #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; #Nginx调用fastcgi接口处理PHP请求。
}
systemctl start nginx
systemctl enable nginx
grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation
vim /usr/share/nginx/html/phpinfo.php
<?php echo phpinfo(); ?>
systemctl start php-fpm
systemctl enable php-fpm
**
基于端口配置
server
listen 81;#修改监听端口
server_name www.wjw.com;
root
/usr/share/nginx/html/wjw1;
Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;
基于域名配置
server
listen 80;
server_name www.wjw.cn;
root
/usr/share/nginx/html/wjw2;
Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw2;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;
基于ip配置
server
listen 192.168.128.130:80;
server_name www.wjw.cn;
root
/usr/share/nginx/html/wjw2;
~~# Load configuration files for the default server block.~~
include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw2;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;
虚拟路径
server
listen 80;
server_name www.wjw1.cn;
root
/usr/share/nginx/html/wjw;
Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw;
Rewrite /wjw/(.*\.php) /$1 break;#重命名wjw
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;
反向代理
server {
listen 80;
server_name 192.168.128.129;
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080
}
}
负载均衡
http {
upstream tomcats {
server 192.168.128.130:8080;
server 192.168.128.131:8080;
server www.wjw.com:8080;
}
server {
listen 80;
location / {
proxy_pass http://tomcats;
}
}
}
Cd /var/www/html/wjw1
Cd /var/www/html/wjw2
Touch index.php
<?Php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) { die("连接失败: " . $conn->connect_error);} $sql = "SELECT id, firstname, lastname FROM MyGuests";$result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; }} else { echo "0 结果";}$conn->close();?>