在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,Igor 将源代码以类BSD许可证的形式发布。 在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。在Nginx在性能方面,在网上有很多的介绍文章,在这里就不重复介绍了。 安装步骤: 一、获取相关开源程序: 2、【适用CentOS、RedHat及其它Linux操作系统】下载程序源码包: 大部份程序下载地址都在国外,下载可能比较慢,也可以去百度云盘下载
tar zxvf libmcrypt-2.5.8.tar.gz tar zxvf mhash-0.9.9.9.tar.gz ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la tar zxvf mcrypt-2.6.8.tar.gz
三、编译安装mariadb(mariadb介绍请访问http://baike.baidu.com/item/mariaDB) ①、创建MySQL数据库存放目录
basedir = /usr/local/mysql datadir = /data/mysql/data log-error = /data/mysql/mysql_error.log pid-file = /data/mysql/mysql.pid
④、创建管理MySQL数据库的shell脚本: vi /data/mysql/mysql 输入以下内容(这里的用户名root和密码12345678接下来的步骤会创建): #!/bin/sh mysql_port=3306 mysql_username="root" mysql_password="123456" function_start_mysql() { printf "Starting MySQL...\n" /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/my.cnf 2>&1 > --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data & } function_stop_mysql() { printf "Stoping MySQL...\n" /usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql sleep 5 function_start_mysql } function_kill_mysql() { kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}') kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}') } if [ "$1" = "start" ]; then function_start_mysql elif [ "$1" = "stop" ]; then function_stop_mysql elif [ "$1" = "restart" ]; then function_restart_mysql elif [ "$1" = "kill" ]; then function_kill_mysql else printf "Usage: /data/mysql/mysql {start|stop|restart|kill}\n" fi
chown -R mysql:mysql /data/mysql
/data/mysql/mysql start
⑦、通过命令行登录管理MySQL服务器(提示输入密码时直接回车): /usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '123456'; ⑨、(可选)停止MySQL: /data/mysql/mysql stop
2、解压安装 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-opcache=yes --with-freetype-dir=/usr/local/freetype --enable-fpm --with-mcrypt --enable-mbstring --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --with-pcre-regex --with-pdo-mysql --enable-mysqlnd --with-mysqli --with-gd --with-jpeg-dir --with-iconv-dir --with-png-dir --with-libxml-dir --enable-xml --enable-zip --enable-calendar --enable-wddx --enable-bcmath --enable-shmop --enable-gd-native-ttf --with-openssl --with-xmlrpc --enable-soap --enable-maintainer-zts --enable-exif --enable-ftp
3、拷贝php.ini等配置文件 4、编译安装PHP5扩展模块 cd /data/soft 如果没有去百度云盘下载压缩包,可用github命令下载 tar zxvf php-memcached.tar.gz
tar zxvf phpredis.tar.gz tar zxvf imagick-3.4.3RC1.tgz 5、修改php.ini文件 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20151012/"
extension = "memcached.so" extension = "imagick.so" extension = "redis.so"
按住shift+g跳到php.ini最后一行,加上opcache相关配置 zend_extension="/usr/local/php/lib/php/extensions/no-debug-zts-20151012/opcache.so" opcache.force_restart_timeout=3600 opcache.memory_consumption=1024 opcache.optimization_level=1 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4096 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable=1 opcache.enable_cli=1
添加php比较危险的函数 disable_functions = exec,system,passthru,ini_alter,dl,openlog,syslog,readlink,symlink,link,leak,proc_open,popepassthru,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,proc_get_status,popen,com,eval,gzuncompress,php_admin_value,php_admin_flag
#!/bin/sh function_start_php() { printf "Starting Php-fpm...\n" /usr/local/php/sbin/php-fpm -g /usr/local/php/var/run/php-fpm.pid } function_stop_php() { printf "Stoping Php-fpm...\n" kill -INT `cat /usr/local/php/var/run/php-fpm.pid` } function_restart_php() { printf "Restarting Php-fpm...\n" kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` } if [ "$1" = "start" ]; then function_start_php elif [ "$1" = "stop" ]; then function_stop_php elif [ "$1" = "restart" ]; then function_restart_php else printf "Usage: /usr/local/php/php-fpm {start|stop|restart}\n" fi
给php-fpm执行以下命令 启动PHP命令
2、安装Tengine 注意:--with-pcre=/data/soft/pcre-8.37指向的是源码包解压的路径,而不是安装的路径,否则会报错,同样,openssl zlib的路径都是源码包解压路径s。 3、创建Nginx日志目录
输入以下内容: user www www; worker_processes 8; error_log /data/logs/nginx_error.log crit; pid /usr/local/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; include vhosts/*.conf; }
创建php5.conf location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; set $path_info ""; set $real_script_name $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { set $real_script_name $1; set $path_info $2; } fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; }
location ~ .*\.(js|css)?$ { expires 1d; }
server { listen 80; charset utf-8; server_name 192.168.1.50; index index.html index.htm index.php; root /data/www/blog; #limit_conn crawler 20; include php5.conf; include image.conf; include js.conf; log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /data/logs/access.log access; }
在/usr/local/nginx/conf/目录中创建fcgi.conf文件: fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; #PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
5、启动Nginx 六、在不停止Nginx服务的情况下平滑变更Nginx配置 如果屏幕显示以下两行信息,说明配置文件正确: 2、平滑重启: 3、创建Nginx启动脚本 #!/bin/sh nginx_s="/usr/local/nginx/sbin/nginx" function_start_nginx() { printf "Starting nginx...\n" ${nginx_s} } function_stop_nginx() { printf "Stoping nginx...\n" ${nginx_s} -s stop } function_restart_nginx() { printf "Restarting nginx...\n" ${nginx_s} -s reload } if [ "$1" = "start" ]; then function_start_nginx elif [ "$1" = "stop" ]; then function_stop_nginx elif [ "$1" = "restart" ]; then function_restart_nginx else printf "Usage: /usr/local/nginx/nginx {start|stop|restart}\n" fi
在末尾增加以下内容: ulimit -SHn 65535 /data/mysql/mysql start /usr/local/php/php-fpm start /usr/local/nginx/sbin/nginx
八、优化Linux内核参数 在末尾增加以下内容: # Add net.ipv4.tcp_max_syn_backlog = 65536 net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_tw_recycle = 1 #net.ipv4.tcp_tw_len = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_max_orphans = 3276800 #net.ipv4.tcp_fin_timeout = 30 #net.ipv4.tcp_keepalive_time = 120 net.ipv4.ip_local_port_range = 1024 65535
使配置立即生效:
输入以下内容: #!/bin/bash # This script run at 00:00 # The Nginx logs path logs_path="/data/logs/" mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log kill -USR1 `cat /usr/local/nginx/nginx.pid`
chmod +x /usr/local/nginx/sbin/cut_nginx_log.sh 2、设置crontab,每天凌晨00:00切割nginx访问日志 输入以下内容: 00 00 * * * /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh |
2022-07-18
2022-08-17
2022-11-06
2022-07-08
2022-08-17
请发表评论