##一、购买一台阿里云的云服务器ECS
购买过程省略
##二、在服务器里面搭建LNMP环境
###1.搭建环境
操作系统 | 主机名 | ip | 内存 |
---|---|---|---|
centos7.5 | web | ecs的公网IP | 1G |
####1.1搭建nginx
[root@zabbix ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@zabbix ~]# setenforce 0
setenforce: SELinux is disabled
[root@zabbix ~]# systemctl stop firewalld
[root@zabbix ~]# systemctl stop NetworkManager
[root@zabbix ~]# ls
anaconda-ks.cfg nginx-1.16.1.tar.gz
[root@zabbix ~]# yum install -y wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
#安装支持程序我们源码编译
[root@web ~]# tar xf nginx-1.16.1.tar.gz -C /usr/src/
[root@webweb ~]# cd /usr/src/nginx-1.16.1/
[root@webweb nginx-1.16.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@webweb nginx-1.16.1]# ./configure --prefix=/usr/local/nginx && make && make install
#编译安装
[root@webweb nginx-1.16.1]# ln -s /usr/local/nginx/sbin/* /usr/bin/ #将命令链接出来
[root@webweb nginx-1.16.1]#which nginx
#查看有了nginx的命令
/usr/bin/nginx
[root@webweb nginx-1.16.1]# cd /usr/local/nginx/conf/
[root@webweb conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@webweb conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
#精简配置文件
[root@webweb conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@web conf]# nginx -t
#检测一下配置文件有没有语法错误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web conf]# /usr/local/nginx/sbin/nginx #建议最后面一步再启动
#启动nginx
[root@web conf]# ss -antup | grep 80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=18266,fd=6),("nginx",pid=18265,fd=6))
#查看下nginx的端口有没有开
#使用systemctl管理nginx
[root@web conf]# vim /usr/lib/systemd/system/nginx.service
[Unit] #这个模块主要是对服务的描述
Description=nginx #描述服务是什么
After=network.target # 描述服务的类别
[Service] #是服务的具体运行参数
Type=forking #后台运行的形式
ExecStart=/usr/local/nginx/sbin/nginx #启动命令
[Install] #服务安装的相关设置
WantedBy=multi-user.target
#测试一下啊systemctl管理(如果这里有问题,请重启下服务器就可以了)
[root@web conf]# ss -antup | grep 80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1213,fd=6),("nginx",pid=1212,fd=6))
[root@web conf]# systemctl stop nginx
[root@web conf]# ss -antup | grep 80web
[root@web conf]#
#开启
[root@web conf]# systemctl start nginx #这里需要注意的是,因为之前nginx已经启动了,所以ngixn的systemctl在这里会失效,最好的办法就是重启服务器,或者不要操作上面的nginx启动
[root@web conf]# ss -antup | grep 80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1249,fd=6),("nginx",pid=1248,fd=6))
1.2关于systemctl管理服务参数说明
[Service]部分是服务的关键,是服务的一些具体运行参数的设置
Type=forking是后台运行的形式,
PIDFile为存放PID的文件路径,
ExecStart为服务的运行命令,
ExecReload为重启命令,
ExecStop为停止命令,
rivateTmp=True表示给服务分配独立的临时空间,
*注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错;
###二搭建php
php和nginx结合使用是有2中方式的,
1是socket方式(这个方式需要两个服务在同一个服务器上)
2是网络方式,这样的可以不在同一个服务其上 (默认是网络)
####2.1编译安装php
[root@web conf]# cd ~
[root@web ~]# wget http://hk1.php.net/distributions/php-5.6.40.tar.gz #现在5.64安装包
[root@web ~]# yum -y install epel-release #用yum安装一个epel源,这个文件是扩展的一些服务的源
[root@web ~]# yum -y clean all
[root@web ~]# yum makecache
[root@web ~]# yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel libmcrypt libmcrypt-devel
[root@web ~]# wget https://www.php.net/distributions/php-5.6.40.tar.gz
[root@web ~]# echo "$?"
0
[root@web ~]# ls
anaconda-ks.cfg nginx-1.16.1.tar.gz php-5.6.40.tar.gz
[root@web ~]# tar xf php-5.6.40.tar.gz -C /usr/src/
[root@web ~]# cd /usr/src/php-5.6.40/
[root@web php-5.6.40]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-ctype --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-fpm
#出现一下的式样就ok了
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
[root@web php-5.6.40]# make && make install
php主要编译安装说明
####2.2启动php
[root@web php-5.6.40]# tail -1 /etc/profile #添加环境变量
export PATH=$PATH:/usr/local/php/sbin/:/usr/local/php/bin/
[root@web php-5.6.40]# source /etc/profile #让他立即生效
[root@web php-5.6.40]# php-fpm -t #检测一下配置文件有没有错误,现在报错是因为没有配置文件在里面,我们需要复制一份过去,
[04-Nov-2019 10:04:15] ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)
[04-Nov-2019 10:04:15] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
[04-Nov-2019 10:04:15] ERROR: FPM initialization failed
[root@web php-5.6.40]# ls php.ini-
php.ini-development php.ini-production
# deve是开发环境的,production是生产环境的
[root@web php-5.6.40]# cp php.ini-production /usr/local/php/etc/php.ini
[root@web php-5.6.40]# cd /usr/local/php/etc/
[root@web etc]# ls
pear.conf php-fpm.conf.default php.ini
[root@web etc]# cp php-fpm.conf.default php-fpm.conf
[root@web etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[root@web etc]# php-fpm -t
[04-Nov-2019 10:16:00] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
[root@web etc]# php-fpm -v
PHP 5.6.40 (fpm-fcgi) (built: Nov 4 2019 09:37:11)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
#使用systemctl将php管理起来,并启动
[root@web etc]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
[Install]
WantedBy=multi-user.target
[root@web etc]# systemctl start php-fpm
[root@web etc]# ss -antup | grep 9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=8872,fd=0),("php-fpm",pid=8871,fd=0),("php-fpm",pid=8870,fd=7))
####2.3修改nginx的配置,让php和nginx连用起来
[root@web etc]# cd /usr/local/nginx/conf/
[root@web conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm index.php; #必须加上.php结尾的主页,要不然nginx调用动态的时候找不到回直接报错就不找了
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #去哪里寻找php文件。
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@web conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web conf]# nginx -s reload
[root@web conf]# cd /usr/local/nginx/html/
[root@webweb html]# touch index.php #里边什么都不用写
[root@web html]# cat test.php
###三mysql数据库安装
####3.1搭建mysql数据库
[root@web html]# cd ~
[root@web ~]# wget https://www.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39.tar.gz
[root@web ~]# ls
anaconda-ks.cfg mysql-5.6.39.tar.gz nginx-1.16.1.tar.gz php-5.6.40.tar.gz
[root@web ~]# yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel
[root@web ~]# useradd -M -s /sbin/nologin mysql
[root@web ~]# tar xf mysql-5.6.39.tar.gz -C /usr/src/
[root@web ~]# cd /usr/src/mysql-5.6.39/
[root@web mysql-5.6.39]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1
[root@web mysql-5.6.39]# echo "$?"
0
[root@web mysql-5.6.39]# make && make install
[root@web mysql-5.6.39]# echo "$?"
0
[root@webweb mysql-5.6.39]# cp support-files/mysql.server /etc/init.d/mysqld
[root@web mysql-5.6.39]# chmod +x /etc/init.d/mysqld
[root@web mysql-5.6.39]# tail -1 /etc/profile #修改环境变量,本质是要服务器找到命令,所以做软连接也是可以的
export PATH=$PATH:/usr/local/mysql/bin/
#修改配置文件
[root@web mysql-5.6.39]# cat /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
user=mysql
skip-name-resolve
long_query_time=2
slow_query_log_file=/data/mysql/mysql-slow.log
expire_logs_days=2
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
max_allowed_packet = 512M
connect_timeout = 60
net_read_timeout = 120
[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
重要的编译选项说明
3.2初始化数据库
[root@webweb mysql-5.6.39]# mkdir -p /data/mysql #建立数据目录
[root@web mysql-5.6.39]# chown -R mysql:mysql /usr/local/mysql /data/mysql/ #分别对数据目录,和运行目录授权
[root@web mysql-5.6.39]# yum install -y perl-Module-Install #初始数据库的一个依赖程序
[root@web mysql-5.6.39]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql/ #初始完成以后输出的特别像乱码,往上看,找到两个ok就初始化完成了
[root@web mysql-5.6.39]# cat /usr/lib/systemd/system/mysqld.service #使用systemctl管理mysql
[Unit]
Description=mysqld
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
[Install]
WantedBy=multi-user.target
[root@web mysql-5.6.39]# systemctl start mysqld
[root@web mysql-5.6.39]# ss -antup | grep 3306
tcp LISTEN 0 80 *:3306 *:* users:(("mysqld",pid=26225,fd=10))
[root@web mysql-5.6.39]# cd ~
[root@web ~]# mysqladmin -h 127.0.0.1 -u root password '123456'
Warning: Using a password on the command line interface can be insecure.
[root@web ~]# echo "$?"
0
[root@web ~]# mysql -uroot -p123456 -h 127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.39 Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.200.%' IDENTIFIED BY '123456' WITH GRANT OPTION; #包括这里的网段和用户,都得根据需求自己写,当然这里你也可以使用超户,拥有一切权限,但是这样不安全,建议创建一个普户,这里我用的是超户,仅仅是为了实验
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
[root@web ~]# mysql -uroot -p123456 -h 192.168.200.173 #验证密码在远处登录,这里的IP地址是mysql所在服务器的IP当然如果你的所有都在一台服务器上面,那面这一步就不用测试
mysql> exit
[root@web ~]# cat /usr/local/nginx/html/test_mysql.php #建立测试nginx+php+mysql 的测试文件
####3.4通过http://192.168.200.173/test_mysqsl.php
三、创建数据库、和需要的表(上面得都是测试LNMP环境)
这里面需要注意的是,创建的数据库表必须是要与你网站网页所需要的库和表要一致,因为网页要往这个里面写数据,包括创建的数据库用户和IP一定要正确,对了还有密码,否则会导致网站无法将数据写入到数据库
[root@web ~]# mysql -uroot -p123456 -h 127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.39 Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.200.%' IDENTIFIED BY '123456' WITH GRANT OPTION; #包括这里的网段和用户,都得根据需求自己写,当然这里你也可以使用超户,拥有一切权限,但是这样不安全,建议创建一个普户,这里我用的是超户,仅仅是为了实验
mysql> create database user_login; #创建我们所需要得库
mysql> use user_login; #进到这个库里面,准备创建我们所需要的表
mysql>create table users(username char(20) not null, password char(30) default '',primary key(username)); #注意这里得表名和字段名都是我们一会就要用的,里面得值可以根据自己得需求自己更改
Query OK, 0 rows affected (0.00 sec)
mysql> desc user_login.users; #查看下我们刚刚创建得表里面得字段是否有误
mysql> insert into users(username,password)values(liushuai,liushuai); #在这里我们先创建一组数据,一会到网页上测试
mysql> select username.password from user_login.users; #再次确认下我们得数据
mysql> flush privileges; #刷新下我们得数据库
Query OK, 0 rows affected (0.00 sec)
mysql> exit
##四、测试数据库是否可以正常连接
同志们在这里自行测试
##五、网页文件/PHP文件
链接:https://pan.baidu.com/s/1Sf_0Oe9fRV54CqCFc_nmgQ
提取码:fy7p
复制这段内容后打开百度网盘手机App,操作更方便哦
##六、注意这里没有具体得写网页得路径
这里得网页路径是/usr/local/nginx/html下面,所以可以下载源码包后可以直接解压,注意解压得时候会多出来一个html目录,记得把之前得那个html删掉再解压
##七、测试
自行测试