利用nginx搭建属于自己的一个网站网址-----亲测有效


一、购买一台阿里云的云服务器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))

关于systemctl管理服务参数说明
[Service]部分是服务的关键,是服务的一些具体运行参数的设置
Type=forking是后台运行的形式,
PIDFile为存放PID的文件路径,
ExecStart为服务的运行命令,
ExecReload为重启命令,
ExecStop为停止命令,
rivateTmp=True表示给服务分配独立的临时空间,
*注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错;

1.2在浏览器上验证下nginx

2.搭建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主要编译安装说明

  • --prefix指定php的安装目录
  • --with-config-file-path指定php的配置文件位置
  • --with-mysql、–with-mysqli让php可以操作mysql
  • --enable-fpm主要是nginx要来调用php语言得使用php-fpm

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.confworker_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
<?phpecho "it's ok";
?>

2.4验证php+nginx是否连接成功

3.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

重要的编译选项说明

  • CMACK_INSTALL_PREFIX指定安装的目录
  • MYSQL_DATADIR指定Mysql的数据目录

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 distributionCopyright (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 的测试文件
<?php
$link=mysql_connect("127.0.0.1","root","zabbixpwd");
if(!$link) echo "FAILD!连接错误,用户名密码不对";
else echo "OK!可以连接";
?>

3.3通过http://192.168.200.173/test_mysqsl.php

4.创建数据库、和需要的表(上面得都是测试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 distributionCopyright (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
4.1测试数据库是否可以正常连接

同志们在这里自行测试

5.网页文件/PHP文件

链接:https://pan.baidu.com/s/1Sf_0Oe9fRV54CqCFc_nmgQ
提取码:fy7p
复制这段内容后打开百度网盘手机App,操作更方便哦

6.注意这里没有具体得写网页得路径

这里得网页路径是/usr/local/nginx/html下面,所以可以下载源码包后可以直接解压,注意解压得时候会多出来一个html目录,记得把之前得那个html删掉再解压

7.测试

自行测试

如何搭建属于自己的一个网站网址-----亲测有效相关推荐

  1. 一个PDF免费转WORD的网站,亲测,好用!

    最近发现一个将pdf免费转word的网站,亲测,很好用,记录下来方便下次使用: https://www.ilovepdf.com/zh-cn

  2. 如何解决电脑的电流声吱吱滋滋和爆破声咔咔,困扰一个月了亲测已经解决

    如何解决电脑的电流声吱吱滋滋和爆破声咔咔,困扰一个月了亲测已经解决 首先声明我的电脑配置: 华硕飞行堡垒FX系列  INTEL 酷睿 I5-4200H  原装硬盘大小 1TB  已使用2年 电脑出现的 ...

  3. 在线编程Python网站,亲测好用

    在线编程Python网站,亲测好用!!! 可能网站有点慢,但是非常好用https://www.onlinegdb.com/online_python_interpreter 在线编程Python网站, ...

  4. 游戏网站搭建,不止是一个网站那么简单

    现在游戏市场的火爆带来了很多新商机,游戏代理也变成了炙手可热的创业项目.尤其对于年轻人来说,本身就玩游戏,又对游戏和网络有所了解,游戏代理听上去是个不错的创业选择. 说到做游戏代理,很多人第一反应是: ...

  5. 8个免费下载文献的学术网站(亲测可用)

    给大家推荐8个免费下载文献的学术网站,希望能帮到大家. 本文首发于公众号智慧科研. 1.Library Genesis Library Genesis号称是帮助全人类知识无版权传播的计划.网站上论文很 ...

  6. hmailserver怎么搭建php,hMailServer邮件服务器安装配置(亲测可用)

    本帖最后由 wsyhj56 于 2015-8-18 12:42 编辑 此篇进入正题,详细的说一下安装和配置过程.得先说一下,hMailServer是真正的邮件服务端,而Roundcube Webmai ...

  7. 免费专利检索和下载网站(亲测超实用)

    更新:2021.2.19 国内官网原来也可以检索并下载,之前没有找到入口: http://pss-system.cnipa.gov.cn/sipopublicsearch/portal/uiIndex ...

  8. Elasticsearch/Kibana 视频学习网址(亲测视频很好)

    @羲凡--只为了更好的活着 Elasticsearch/Kibana 视频学习网址 一个神奇的网址 一个良心的视频 https://www.iqiyi.com/v_19rr1myo5c.html#cu ...

  9. 红包封面发货平台卡密系统全新红包封面平台可搭建分站独立后台的源码-亲测可用

    红包封面发货平台 卡密系统 全新红包封面平台 可搭建分站 独立后台的源码下载 1.此款为红包封面发货平台源码 2.平台支持自定义分站 3.拥有独立后台,独立版权 4.兼容性强虚拟主机也可部署 5.20 ...

最新文章

  1. python3 执行系统命令
  2. 在大数据圈你不知道的15个新技术
  3. spingmvc的一些简单理解和记录
  4. boost::container_hash模块实现哈希序列
  5. java创建一个不可变对象_使用不可变对象创建值对象
  6. Mysql学习总结(60)——并发量大、数据量大的互联网业务数据库设计规范总结
  7. CCNA之单臂路由实验
  8. mac显示网速_Mac系统怎么查看测试网速
  9. objective-c和java哪个简单_Objective-C和Java的简单对比
  10. java中substring与substr的用法
  11. linux下编译yacc命令,Lex/Yacc的学习——《编译原理及实践》附录B tiny编译器源码在linux下编译实现...
  12. AOSP 隐藏 su
  13. 远程桌面3389加固
  14. 计算机任意字符替换,Word中级技巧之同类字符的精确替换
  15. 管理与维护linux系统(任务五 六 七 八)
  16. 自动化控制重要国际学术会议
  17. c语言二级程序设计题难吗,c语言二级公共基础知识试题特点
  18. Dennard Scaling
  19. mysql的基础命令之更改密码
  20. Unity游戏制作问题整理(1)--添加按钮声音

热门文章

  1. java财务管理源代码_java 个人财务管理系统 入门级源码
  2. sap查询所有事物代码
  3. IDEA 打不开,双击无反应,IDEA 2021.3
  4. BZOJ 3609: [Heoi2014]人人尽说江南好
  5. android div拖动排序,鼠标拖动实现DIV排序示例代码
  6. ubuntu18连不上安卓手机的USB网络共享
  7. 完全开源Android项目:PDF电子书架软件——不动的大图书馆
  8. 鼠标右键失灵及间歇性失灵,解决方法
  9. ROS掉包侠修炼计划
  10. 居然之家联手分众巨额投入:引爆2亿目标人群 双11力争破百亿