1,环境搭建准备

安装包 版本
MySQL mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
PHP php-5.6.30.tar.gz
Nginx nginx-1.17.8.tar.gz

2,安装MySQL及配置

  1. //上传安装包
  2. //移动到目录下
  3. [root@localhost ~]# cd /usr/local/src/
  4. [root@localhost src]# ls
  5. mysql-5.6.47-linux-glibc2.12-x86_64
  6. //安装常用工具
  7. [root@localhost src]# yum install -y vim net_tools tree
  8. [root@lnmp src]# tar -zxf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  9. # 移动
  10. [root@lnmp src]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
  11. # 创建MySQL用户,不要登录,启动的时候登录
  12. [root@lnmp src]# useradd -s /sbin/nologin mysql
  13. [root@lnmp src]# cd /usr/local/mysql/
  14. # 创建存放数据目录;更改权限
  15. [root@lnmp mysql]# mkdir -p /data/mysql
  16. [root@lnmp mysql]# chown -R mysql:mysql /data/mysql/
  17. [root@lnmp mysql]# cd /data/mysql/
  18. [root@lnmp mysql]# ls -la
  19. total 2
  20. drwxr-xr-x. 5 mysql mysql 104 Aug 9 18:10 .
  21. drwxr-xr-x. 3 root root 18 Aug 9 18:05 ..
  22. [root@lnmp mysql]# cd /usr/local/mysql/
  23. # 安装所需要依赖
  24. [root@lnmp mysql]# yum install -y perl-Module-Install
  25. # 初始化,上面有两个ok确定安装成功
  26. [root@lnmp mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/

配置MySQL文件

  1. //复制更改配置文件
  2. [root@lnmp mysql]# cp support-files/my-default.cnf /etc/my.cnf
  3. cp: overwrite ‘/etc/my.cnf’? y
  4. [root@lnmp mysql]# vim /etc/my.cnf
  5. basedir = /usr/local/mysql
  6. datadir = /data/mysql
  7. port = 3306
  8. server_id = 29
  9. socket = /tmp/mysql.sock
  10. //复制启动脚本
  11. [root@lnmp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  12. //赋予权限
  13. [root@lnmp mysql]# chmod 777 /etc/init.d/mysqld
  14. //配置启动文件
  15. [root@lnmp mysql]# vim /etc/init.d/mysqld
  16. basedir=/usr/local/mysql
  17. datadir=/data/mysql
  18. //设计开机自启动
  19. [root@lnmp mysql]# chkconfig --add mysqld
  20. //重启
  21. [root@lnmp mysql]# chkconfig mysqld on
  22. [root@lnmp mysql]# service mysqld start
  23. Starting MySQL. SUCCESS!
  24. //查看端口
  25. [root@lnmp mysql]# netstat -ntlp
  26. Active Internet connections (only servers)
  27. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  28. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1462/sshd
  29. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2605/master
  30. tcp6 0 0 :::3306 :::* LISTEN 7891/mysqld
  31. tcp6 0 0 :::22 :::* LISTEN 1462/sshd
  32. tcp6 0 0 ::1:25 :::* LISTEN 2605/master

3,安装PHP及配置

  1. //移动
  2. [root@lnmp src]# cd /usr/local/src/
  3. [root@lnmp src]# ls
  4. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz php-5.6.30.tar.gz
  5. //安装依赖
  6. [root@lnmp src]# yum install -y gcc libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel eplel-release libmcrypt-devel libcurl-devel libjpeg-devel
  7. //解压PHP
  8. [root@lnmp src]# tar -zxf php-5.6.30.tar.gz
  9. //移动
  10. [root@lnmp src]# cd php-5.6.30
  11. //编译
  12. [root@lnmp php-5.6.30]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl
  13. //如果发生这样错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
  14. #yum install -y epel-release
  15. #yum install -y libmcrypt-devel
  16. 两个不能一起安装,因为CentOs6默认的yum源没有 libmcrypt-devel这个包,只能借助epelyum源,所以先安装epel,再安装libmcrypt
  17. //安装
  18. [root@lnmp php-5.6.30]# make && make install
  19. //检查一下
  20. [root@lnmp php-5.6.30]# echo $?
  21. 0

配置PHP文件

  1. // 复制配置文件
  2. [root@lnmp php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
  3. // 修改配置文件
  4. [root@lnmp php-5.6.30]# vim /usr/local/php-fpm/etc/php-fpm.conf
  5. [global]
  6. pid = /usr/local/php-fpm/var/run/php-fpm.pid
  7. error_log = /usr/local/php-fpm/var/log/php-fpm.log
  8. [www]
  9. listen = /tmp/php-fcgi.sock
  10. listen.mode = 666
  11. user = php-fpm
  12. group = php-fpm
  13. pm = dynamic
  14. pm.max_children = 50
  15. pm.start_servers = 20
  16. pm.min_spare_servers = 5
  17. pm.max_spare_servers = 35
  18. pm.max_requests = 500
  19. rlimit_files = 1024
  20. //创建用户
  21. [root@lnmp php-5.6.30]# useradd -s /sbin/nologin php-fpm
  22. // 检查配置文件,带有successful 表示成功。
  23. [root@lnmp php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
  24. [09-Aug-2021 21:21:15] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
  25. //启动php-fpm
  26. [root@lnmp php-5.6.30]# cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  27. //赋予权限
  28. [root@lnmp php-5.6.30]# chmod 755 /etc/init.d/php-fpm
  29. [root@lnmp php-5.6.30]# service php-fpm start
  30. Starting php-fpm done
  31. //启动PHP-fpm
  32. [root@lnmp php-5.6.30]# chkconfig php-fpm on
  33. //查看端口
  34. [root@lnmp php-5.6.30]# ps aux |grep php-fpm
  35. root 6970 0.0 0.2 128280 5076 ? Ss 22:11 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
  36. php-fpm 6971 0.0 0.2 128280 4812 ? S 22:11 0:00 php-fpm: pool www
  37. php-fpm 6972 0.0 0.2 128280 4812 ? S 22:11 0:00 php-fpm: pool www
  38. php-fpm 6973 0.0 0.2 128280 4812 ? S 22:11 0:00 php-fpm: pool www
  39. php-fpm 6974 0.0 0.2 128280 4812 ? S 22:11 0:00 php-fpm: pool www
  40. php-fpm 6975 0.0 0.2 128280 4816 ? S 22:11 0:00 php-fpm: pool www
  41. php-fpm 6976 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  42. php-fpm 6977 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  43. php-fpm 6978 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  44. php-fpm 6979 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  45. php-fpm 6980 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  46. php-fpm 6981 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  47. php-fpm 6982 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  48. php-fpm 6983 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  49. php-fpm 6984 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  50. php-fpm 6985 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  51. php-fpm 6986 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  52. php-fpm 6987 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  53. php-fpm 6988 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  54. php-fpm 6989 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  55. php-fpm 6990 0.0 0.2 128280 4820 ? S 22:11 0:00 php-fpm: pool www
  56. root 7013 0.0 0.0 112828 980 pts/0 R+ 22:14 0:00 grep --color=auto php-fpm

4,安装Nginx及配置

  1. //移动到目录下
  2. [root@lnmp ~]# cd /usr/local/src/
  3. [root@lnmp src]# ls
  4. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz php-5.6.30
  5. nginx-1.17.8.tar.gz php-5.6.30.tar.gz
  6. //je
  7. [root@lnmp src]# tar zxf nginx-1.17.8.tar.gz
  8. [root@lnmp src]# cd nginx-1.17.8
  9. [root@lnmp nginx-1.17.8]# ./configure --prefix=/usr/local/nginx
  10. //相比MySQL,PHP Nginx所需的内容较少,速度较快
  11. [root@lnmp nginx-1.17.8]# make && make install
  12. //检查
  13. [root@lnmp nginx-1.17.8]# echo $?
  14. 0
  15. //编辑配置文件
  16. [root@lnmp nginx-1.17.8]# vim /etc/init.d/nginx
  17. #!/bin/bash
  18. # chkconfig: - 30 21
  19. # description: http service.
  20. # Source Function Library
  21. . /etc/init.d/functions
  22. # Nginx Settings
  23. NGINX_SBIN="/usr/local/nginx/sbin/nginx"
  24. NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
  25. NGINX_PID="/usr/local/nginx/logs/nginx.pid"
  26. RETVAL=0
  27. prog="Nginx"
  28. start()
  29. {
  30. echo -n $"Starting $prog: "
  31. mkdir -p /dev/shm/nginx_temp
  32. daemon $NGINX_SBIN -c $NGINX_CONF
  33. RETVAL=$?
  34. echo
  35. return $RETVAL
  36. }
  37. stop()
  38. {
  39. echo -n $"Stopping $prog: "
  40. killproc -p $NGINX_PID $NGINX_SBIN -TERM
  41. rm -rf /dev/shm/nginx_temp
  42. RETVAL=$?
  43. echo
  44. return $RETVAL
  45. }
  46. reload()
  47. {
  48. echo -n $"Reloading $prog: "
  49. killproc -p $NGINX_PID $NGINX_SBIN -HUP
  50. RETVAL=$?
  51. echo
  52. return $RETVAL
  53. }
  54. restart()
  55. {
  56. stop
  57. start
  58. }
  59. configtest()
  60. {
  61. $NGINX_SBIN -c $NGINX_CONF -t
  62. return 0
  63. }
  64. case "$1" in
  65. start)
  66. start
  67. ;;
  68. stop)
  69. stop
  70. ;;
  71. reload)
  72. reload
  73. ;;
  74. restart)
  75. restart
  76. ;;
  77. configtest)
  78. configtest
  79. ;;
  80. *)
  81. echo $"Usage: $0 {start|stop|reload|restart|configtest}"
  82. RETVAL=1
  83. esac
  84. exit $RETVAL
  85. [root@lnmp nginx-1.17.8]# chmod 755 /etc/init.d/nginx
  86. [root@lnmp nginx-1.17.8]# chkconfig --add nginx
  87. [root@lnmp nginx-1.17.8]# chkconfig nginx on
  88. [root@lnmp nginx-1.17.8]# > /usr/local/nginx/conf/nginx.conf
  89. [root@lnmp nginx-1.17.8]# vim /usr/local/nginx/conf/nginx.conf
  90. user nobody nobody;
  91. worker_processes 2;
  92. error_log /usr/local/nginx/logs/nginx_error.log crit;
  93. pid /usr/local/nginx/logs/nginx.pid;
  94. worker_rlimit_nofile 51200;
  95. events
  96. {
  97. use epoll;
  98. worker_connections 6000;
  99. }
  100. http
  101. {
  102. include mime.types;
  103. default_type application/octet-stream;
  104. server_names_hash_bucket_size 3526;
  105. server_names_hash_max_size 4096;
  106. log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  107. ' $host "$request_uri" $status'
  108. ' "$http_referer" "$http_user_agent"';
  109. sendfile on;
  110. tcp_nopush on;
  111. keepalive_timeout 30;
  112. client_header_timeout 3m;
  113. client_body_timeout 3m;
  114. send_timeout 3m;
  115. connection_pool_size 256;
  116. client_header_buffer_size 1k;
  117. large_client_header_buffers 8 4k;
  118. request_pool_size 4k;
  119. output_buffers 4 32k;
  120. postpone_output 1460;
  121. client_max_body_size 10m;
  122. client_body_buffer_size 256k;
  123. client_body_temp_path /usr/local/nginx/client_body_temp;
  124. proxy_temp_path /usr/local/nginx/proxy_temp;
  125. fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  126. fastcgi_intercept_errors on;
  127. tcp_nodelay on;
  128. gzip on;
  129. gzip_min_length 1k;
  130. gzip_buffers 4 8k;
  131. gzip_comp_level 5;
  132. gzip_http_version 1.1;
  133. gzip_types text/plain application/x-javascript text/css text/htm
  134. application/xml;
  135. server
  136. {
  137. listen 80;
  138. server_name localhost;
  139. index index.html index.htm index.php;
  140. root /usr/local/nginx/html;
  141. location ~ \.php$
  142. {
  143. include fastcgi_params;
  144. fastcgi_pass unix:/tmp/php-fcgi.sock;
  145. fastcgi_index index.php;
  146. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
  147. }
  148. }
  149. }
  150. //检查重启
  151. [root@lnmp nginx-1.17.8]# /usr/local/nginx/sbin/nginx -t
  152. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  153. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  154. // 启动服务
  155. [root@lnmp nginx-1.17.8]# service nginx start
  156. Starting nginx (via systemctl): [ OK ]
  157. //查看Nginx端口
  158. [root@ln[root@bai nginx-1.17.8]# ps aux |grep nginx
  159. root 9638 0.0 0.0 20572 636 ? Ss 22:33 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  160. nobody 9639 0.0 0.1 22968 3196 ? S 22:33 0:00 nginx: worker process
  161. nobody 9640 0.0 0.1 22968 3196 ? S 22:33 0:00 nginx: worker process
  162. root 9645 0.0 0.0 112828 976 pts/0 S+ 22:36 0:00 grep --color=auto nginx
  163. mp nginx-1.17.8]# ps aux |grep nginx
  164. //查看端口
  165. [root@lnmp nginx-1.17.8]# netstat -ntlp
  166. Active Internet connections (only servers)
  167. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  168. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9638/nginx: master
  169. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1468/sshd
  170. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1932/master
  171. tcp6 0 0 :::3306 :::* LISTEN 10412/mysqld
  172. tcp6 0 0 :::22 :::* LISTEN 1468/sshd
  173. tcp6 0 0 ::1:25 :::* LISTEN 1932/master
  174. //检验
  175. [root@lnmp nginx-1.17.8]# curl localhost
  176. <!DOCTYPE html>
  177. <html>
  178. <head>
  179. <title>Welcome to nginx!</title>
  180. <style>
  181. body {
  182. width: 35em;
  183. margin: 0 auto;
  184. font-family: Tahoma, Verdana, Arial, sans-serif;
  185. }
  186. </style>
  187. </head>
  188. <body>
  189. <h1>Welcome to nginx!</h1>
  190. <p>If you see this page, the nginx web server is successfully installed and
  191. working. Further configuration is required.</p>
  192. <p>For online documentation and support please refer to
  193. <a href="http://nginx.org/">nginx.org</a>.<br/>
  194. Commercial support is available at
  195. <a href="http://nginx.com/">nginx.com</a>.</p>
  196. <p><em>Thank you for using nginx.</em></p>
  197. </body>
  198. </html>

测试PHP能否解析

  1. //测试能否正确解析PHP
  2. [root@lnmp nginx-1.17.8]# vim /usr/local/nginx/html/1.php
  3. <?php
  4. echo "test php scripts"
  5. ?>
  6. [root@lnmp nginx-1.17.8]# curl localhost/1.php
  7. test php scripts[root@lnmp nginx-1.17.8]#
  8. //测试成功。