环境
Linux版本:CentOS7.6 64位
db:192.128.90.171
安装前准备工作
1.关闭selinux
[root@db ~]# vim /etc/selinux/config
SELINUX=disabled
2.关闭firewall,清空iptables
[root@db ~]# systemctl stop firewalld.service
[root@db ~]# systemctl disable firewalld.service
[root@db ~]# iptables -L
[root@db ~]# iptables -F
3.安装LNMP环境依赖软件包
[root@db ~]# yum -y install bzip2-devel curl-devel freetype-devel gcc libjpeg-devel libpng-devel libxslt-devel libxml2-devel openssl-devel pcre-devel pcre-devel zlib-devel
编译安装nginx
1.下载nginx-1.16.0.tar.gz
[root@db ~]# wget https://nginx.org/download/nginx-1.16.0.tar.gz
2.解压nginx包并配置安装
[root@db ~]# tar -zxvf nginx-1.16.0.tar.gz
[root@db ~]# cd nginx-1.16.0
[root@db nginx-1.16.0]# ./configure --prefix=/usr/local/nginx
[root@db nginx-1.16.0]# make
[root@db nginx-1.16.0]# make install
3.测试是否安装成功
[root@db ~]# cd /usr/local/nginx/
[root@db ~]# /usr/local/nginx/sbin/nginx -t
[root@db ~]# /usr/local/nginx/sbin/nginx
浏览器输入服务器的IP地址:192.168.90.171
4.修改网站的根目录为:/webapp/html
[root@db nginx]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.90.171;
root /webapp/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
index index.php index.html index.htm;
}
[root@db ~]# mkdir -p /webapp/html
[root@db ~]# cd /webapp/html/
[root@db html]# vim index.html
<p1>welcome to nginx</p1>
[root@db ~]# /usr/local/nginx/sbin/nginx -s reload
测试:
浏览器输入服务器的IP地址:192.168.90.171/index.html
5.添加nginx到系统服务
[root@db ~]# /usr/local/nginx/sbin/nginx -s quit
[root@db ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6.启动nginx且添加开机自启
[root@db ~]# systemctl start nginx
[root@db ~]# systemctl enable nginx
编译安装PHP
1.下载php-7.0.11.tar.gz
下载地址:http://php.net/get/php-7.0.11.tar.gz/from/a/mirror,下载在本地然后上传到服务器
[root@db ~]# rz
2.解压安装包并配置安装
[root@db ~]# tar -zxvf php-7.0.11.tar.gz
[root@db ~]# cd php-7.0.11
[root@db php-7.0.11]# ./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
[root@db php-7.0.11]# make
[root@db php-7.0.11]# make install
3.配置PHP
[root@db php-7.0.11]# cp php.ini-development /usr/local/php/php.ini
[root@db php-7.0.11]# cd /usr/local/php/
[root@db php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@db php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
[root@db php]# vim php.ini
cgi.fix_pathinfo=0
[root@db php]# /usr/local/php/sbin/php-fpm
[root@db php]# netstat -nplt | grep 9000
4.配置nginx联动PHP
[root@db ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.90.171;
root /webapp/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#添加index.php
index index.php index.html index.htm;
}
location ~ \.php$ {
root /webapp/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[root@db ~]# /usr/local/nginx/sbin/nginx -s reload
5.测试
[root@db ~]# vim /webapp/html/index.php
<?php
phpinfo();
?>
然后打开浏览器输入对应的地址进行访问:http://192.168.90.171/index.php
6.添加php-fpm到系统服务
[root@db ~]# kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
[root@db ~]# vim /lib/systemd/system/php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
7.启动php-fpm且添加开机自启
[root@db ~]# systemctl start php-fpm.service
[root@db ~]# systemctl enable php-fpm.service
MySQL5.7安装
使用MySQL Yum存储库的快速安装
1.将MySQL Yum存储库添加到系统的存储库列表中
a.访问https://dev.mysql.com/downloads/repo/yum/上的MySQL Yum存储库下载页面 。
b.选择并下载适用于您的平台的发行包。
c.使用以下命令安装下载的发行包,替换 platform-and-version-specific-package-name 为下载的包的名称:
[root@db ~]# rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
2.选择mysql5.7版本安装
[root@db ~]# yum repolist all | grep mysql
[root@db ~]# yum-config-manager --disable mysql80-community
[root@db ~]# yum-config-manager --enable mysql57-community
[root@db ~]# yum repolist enabled | grep mysq
3.安装MySQL
[root@db ~]# yum -y install mysql-community-server
4.启动MySQL并设置开机自启
[root@db ~]# systemctl start mysqld.service
[root@db ~]# systemctl enable mysqld.service
5.修改mysql用户root密码
[root@db ~]# grep 'temporary password' /var/log/mysqld.log
[root@db ~]# mysql -u root -pG9U:=dfHK:u8
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Hl_123456';
mysql> flush privileges;
6.测试PHP连接MySQL
[root@db ~]# mysql -u root -pHl_123456
mysql> create database test;
mysql> use test;
mysql> create table tb_user( id int(10) not null auto_increment primary key, username varchar(64) default '' );
mysql> insert into tb_user (id,username) values(1,'xiaohuang');
mysql> insert into tb_user (id,username) values(2,'xiaodong');
mysql> exit
[root@db ~]# vim /webapp/html/mysqli.php
<?php
/*
* mysqli
* 数据库地址,登陆账号,密码,数据库名称
*/
$mysqli = new mysqli("127.0.0.1", "root", "Hl_123456", "test");
if($mysqli){
echo 'Connected to MySQL Success.';
}else{
echo 'Connected to MySQL Fail.';
}
echo '<hr/>';
$sql = "SELECT * FROM tb_user";
$result = $mysqli->query($sql);
if ($result) {
while ($row = $result->fetch_assoc()) {
echo 'Username: '.$row['username']. '<br/>';
}
}
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>
打开浏览器访问测试结果如下:http://192.168.90.171/mysqli.php
配置nginx多目录域名站点
1.创建域名目录并授权
[root@db ~]# mkdir /www/{web,blog} -p
[root@db ~]# chown -R nginx.nginx /www
[root@db ~]# echo "This is a web!" >> /www/web/index.html
[root@db ~]# echo "This is a blog!" >> /www/blog/index.html
2.修改配置文件nginx.conf并重启服务
[root@db ~]# vim /usr/local/nginx/conf/nginx.conf
#在配置文件中建立多个server配置,在server配置中用server_name来对域名信息进行过滤
server
{
listen 80;
server_name www.web01.com; #绑定域名
index index.htm index.html index.php; #默认文件
root /www/web; #网站根目录
include location.conf; #调用其他规则,也可去除
}
server
{
listen 80;
server_name www.blog01.com; #绑定域名
index index.htm index.html index.php; #默认文件
root /www/blog; #网站根目录
include location.conf; #调用其他规则,也可去除
}
[root@db ~]# /usr/local/nginx/sbin/nginx -t
[root@db ~]# /usr/local/nginx/sbin/nginx -s reload
3.测试
[root@db ~]# curl www.web01.com
This is a web!
[root@db ~]# curl www.blog01.com
This is a blog!